Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: resolv.rb
require 'socket'
[0] Fix | Delete
require 'fcntl'
[1] Fix | Delete
require 'timeout'
[2] Fix | Delete
require 'thread'
[3] Fix | Delete
[4] Fix | Delete
begin
[5] Fix | Delete
require 'securerandom'
[6] Fix | Delete
rescue LoadError
[7] Fix | Delete
end
[8] Fix | Delete
[9] Fix | Delete
# Resolv is a thread-aware DNS resolver library written in Ruby. Resolv can
[10] Fix | Delete
# handle multiple DNS requests concurrently without blocking. The ruby
[11] Fix | Delete
# interpreter.
[12] Fix | Delete
#
[13] Fix | Delete
# See also resolv-replace.rb to replace the libc resolver with # Resolv.
[14] Fix | Delete
#
[15] Fix | Delete
# Resolv can look up various DNS resources using the DNS module directly.
[16] Fix | Delete
#
[17] Fix | Delete
# Examples:
[18] Fix | Delete
#
[19] Fix | Delete
# p Resolv.getaddress "www.ruby-lang.org"
[20] Fix | Delete
# p Resolv.getname "210.251.121.214"
[21] Fix | Delete
#
[22] Fix | Delete
# Resolv::DNS.open do |dns|
[23] Fix | Delete
# ress = dns.getresources "www.ruby-lang.org", Resolv::DNS::Resource::IN::A
[24] Fix | Delete
# p ress.map { |r| r.address }
[25] Fix | Delete
# ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::IN::MX
[26] Fix | Delete
# p ress.map { |r| [r.exchange.to_s, r.preference] }
[27] Fix | Delete
# end
[28] Fix | Delete
#
[29] Fix | Delete
#
[30] Fix | Delete
# == Bugs
[31] Fix | Delete
#
[32] Fix | Delete
# * NIS is not supported.
[33] Fix | Delete
# * /etc/nsswitch.conf is not supported.
[34] Fix | Delete
[35] Fix | Delete
class Resolv
[36] Fix | Delete
[37] Fix | Delete
##
[38] Fix | Delete
# Looks up the first IP address for +name+.
[39] Fix | Delete
[40] Fix | Delete
def self.getaddress(name)
[41] Fix | Delete
DefaultResolver.getaddress(name)
[42] Fix | Delete
end
[43] Fix | Delete
[44] Fix | Delete
##
[45] Fix | Delete
# Looks up all IP address for +name+.
[46] Fix | Delete
[47] Fix | Delete
def self.getaddresses(name)
[48] Fix | Delete
DefaultResolver.getaddresses(name)
[49] Fix | Delete
end
[50] Fix | Delete
[51] Fix | Delete
##
[52] Fix | Delete
# Iterates over all IP addresses for +name+.
[53] Fix | Delete
[54] Fix | Delete
def self.each_address(name, &block)
[55] Fix | Delete
DefaultResolver.each_address(name, &block)
[56] Fix | Delete
end
[57] Fix | Delete
[58] Fix | Delete
##
[59] Fix | Delete
# Looks up the hostname of +address+.
[60] Fix | Delete
[61] Fix | Delete
def self.getname(address)
[62] Fix | Delete
DefaultResolver.getname(address)
[63] Fix | Delete
end
[64] Fix | Delete
[65] Fix | Delete
##
[66] Fix | Delete
# Looks up all hostnames for +address+.
[67] Fix | Delete
[68] Fix | Delete
def self.getnames(address)
[69] Fix | Delete
DefaultResolver.getnames(address)
[70] Fix | Delete
end
[71] Fix | Delete
[72] Fix | Delete
##
[73] Fix | Delete
# Iterates over all hostnames for +address+.
[74] Fix | Delete
[75] Fix | Delete
def self.each_name(address, &proc)
[76] Fix | Delete
DefaultResolver.each_name(address, &proc)
[77] Fix | Delete
end
[78] Fix | Delete
[79] Fix | Delete
##
[80] Fix | Delete
# Creates a new Resolv using +resolvers+.
[81] Fix | Delete
[82] Fix | Delete
def initialize(resolvers=[Hosts.new, DNS.new])
[83] Fix | Delete
@resolvers = resolvers
[84] Fix | Delete
end
[85] Fix | Delete
[86] Fix | Delete
##
[87] Fix | Delete
# Looks up the first IP address for +name+.
[88] Fix | Delete
[89] Fix | Delete
def getaddress(name)
[90] Fix | Delete
each_address(name) {|address| return address}
[91] Fix | Delete
raise ResolvError.new("no address for #{name}")
[92] Fix | Delete
end
[93] Fix | Delete
[94] Fix | Delete
##
[95] Fix | Delete
# Looks up all IP address for +name+.
[96] Fix | Delete
[97] Fix | Delete
def getaddresses(name)
[98] Fix | Delete
ret = []
[99] Fix | Delete
each_address(name) {|address| ret << address}
[100] Fix | Delete
return ret
[101] Fix | Delete
end
[102] Fix | Delete
[103] Fix | Delete
##
[104] Fix | Delete
# Iterates over all IP addresses for +name+.
[105] Fix | Delete
[106] Fix | Delete
def each_address(name)
[107] Fix | Delete
if AddressRegex =~ name
[108] Fix | Delete
yield name
[109] Fix | Delete
return
[110] Fix | Delete
end
[111] Fix | Delete
yielded = false
[112] Fix | Delete
@resolvers.each {|r|
[113] Fix | Delete
r.each_address(name) {|address|
[114] Fix | Delete
yield address.to_s
[115] Fix | Delete
yielded = true
[116] Fix | Delete
}
[117] Fix | Delete
return if yielded
[118] Fix | Delete
}
[119] Fix | Delete
end
[120] Fix | Delete
[121] Fix | Delete
##
[122] Fix | Delete
# Looks up the hostname of +address+.
[123] Fix | Delete
[124] Fix | Delete
def getname(address)
[125] Fix | Delete
each_name(address) {|name| return name}
[126] Fix | Delete
raise ResolvError.new("no name for #{address}")
[127] Fix | Delete
end
[128] Fix | Delete
[129] Fix | Delete
##
[130] Fix | Delete
# Looks up all hostnames for +address+.
[131] Fix | Delete
[132] Fix | Delete
def getnames(address)
[133] Fix | Delete
ret = []
[134] Fix | Delete
each_name(address) {|name| ret << name}
[135] Fix | Delete
return ret
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
##
[139] Fix | Delete
# Iterates over all hostnames for +address+.
[140] Fix | Delete
[141] Fix | Delete
def each_name(address)
[142] Fix | Delete
yielded = false
[143] Fix | Delete
@resolvers.each {|r|
[144] Fix | Delete
r.each_name(address) {|name|
[145] Fix | Delete
yield name.to_s
[146] Fix | Delete
yielded = true
[147] Fix | Delete
}
[148] Fix | Delete
return if yielded
[149] Fix | Delete
}
[150] Fix | Delete
end
[151] Fix | Delete
[152] Fix | Delete
##
[153] Fix | Delete
# Indicates a failure to resolve a name or address.
[154] Fix | Delete
[155] Fix | Delete
class ResolvError < StandardError; end
[156] Fix | Delete
[157] Fix | Delete
##
[158] Fix | Delete
# Indicates a timeout resolving a name or address.
[159] Fix | Delete
[160] Fix | Delete
class ResolvTimeout < TimeoutError; end
[161] Fix | Delete
[162] Fix | Delete
##
[163] Fix | Delete
# DNS::Hosts is a hostname resolver that uses the system hosts file.
[164] Fix | Delete
[165] Fix | Delete
class Hosts
[166] Fix | Delete
if /mswin32|mingw|bccwin/ =~ RUBY_PLATFORM
[167] Fix | Delete
require 'win32/resolv'
[168] Fix | Delete
DefaultFileName = Win32::Resolv.get_hosts_path
[169] Fix | Delete
else
[170] Fix | Delete
DefaultFileName = '/etc/hosts'
[171] Fix | Delete
end
[172] Fix | Delete
[173] Fix | Delete
##
[174] Fix | Delete
# Creates a new DNS::Hosts, using +filename+ for its data source.
[175] Fix | Delete
[176] Fix | Delete
def initialize(filename = DefaultFileName)
[177] Fix | Delete
@filename = filename
[178] Fix | Delete
@mutex = Mutex.new
[179] Fix | Delete
@initialized = nil
[180] Fix | Delete
end
[181] Fix | Delete
[182] Fix | Delete
def lazy_initialize # :nodoc:
[183] Fix | Delete
@mutex.synchronize {
[184] Fix | Delete
unless @initialized
[185] Fix | Delete
@name2addr = {}
[186] Fix | Delete
@addr2name = {}
[187] Fix | Delete
open(@filename) {|f|
[188] Fix | Delete
f.each {|line|
[189] Fix | Delete
line.sub!(/#.*/, '')
[190] Fix | Delete
addr, hostname, *aliases = line.split(/\s+/)
[191] Fix | Delete
next unless addr
[192] Fix | Delete
addr.untaint
[193] Fix | Delete
hostname.untaint
[194] Fix | Delete
@addr2name[addr] = [] unless @addr2name.include? addr
[195] Fix | Delete
@addr2name[addr] << hostname
[196] Fix | Delete
@addr2name[addr] += aliases
[197] Fix | Delete
@name2addr[hostname] = [] unless @name2addr.include? hostname
[198] Fix | Delete
@name2addr[hostname] << addr
[199] Fix | Delete
aliases.each {|n|
[200] Fix | Delete
n.untaint
[201] Fix | Delete
@name2addr[n] = [] unless @name2addr.include? n
[202] Fix | Delete
@name2addr[n] << addr
[203] Fix | Delete
}
[204] Fix | Delete
}
[205] Fix | Delete
}
[206] Fix | Delete
@name2addr.each {|name, arr| arr.reverse!}
[207] Fix | Delete
@initialized = true
[208] Fix | Delete
end
[209] Fix | Delete
}
[210] Fix | Delete
self
[211] Fix | Delete
end
[212] Fix | Delete
[213] Fix | Delete
##
[214] Fix | Delete
# Gets the IP address of +name+ from the hosts file.
[215] Fix | Delete
[216] Fix | Delete
def getaddress(name)
[217] Fix | Delete
each_address(name) {|address| return address}
[218] Fix | Delete
raise ResolvError.new("#{@filename} has no name: #{name}")
[219] Fix | Delete
end
[220] Fix | Delete
[221] Fix | Delete
##
[222] Fix | Delete
# Gets all IP addresses for +name+ from the hosts file.
[223] Fix | Delete
[224] Fix | Delete
def getaddresses(name)
[225] Fix | Delete
ret = []
[226] Fix | Delete
each_address(name) {|address| ret << address}
[227] Fix | Delete
return ret
[228] Fix | Delete
end
[229] Fix | Delete
[230] Fix | Delete
##
[231] Fix | Delete
# Iterates over all IP addresses for +name+ retrieved from the hosts file.
[232] Fix | Delete
[233] Fix | Delete
def each_address(name, &proc)
[234] Fix | Delete
lazy_initialize
[235] Fix | Delete
if @name2addr.include?(name)
[236] Fix | Delete
@name2addr[name].each(&proc)
[237] Fix | Delete
end
[238] Fix | Delete
end
[239] Fix | Delete
[240] Fix | Delete
##
[241] Fix | Delete
# Gets the hostname of +address+ from the hosts file.
[242] Fix | Delete
[243] Fix | Delete
def getname(address)
[244] Fix | Delete
each_name(address) {|name| return name}
[245] Fix | Delete
raise ResolvError.new("#{@filename} has no address: #{address}")
[246] Fix | Delete
end
[247] Fix | Delete
[248] Fix | Delete
##
[249] Fix | Delete
# Gets all hostnames for +address+ from the hosts file.
[250] Fix | Delete
[251] Fix | Delete
def getnames(address)
[252] Fix | Delete
ret = []
[253] Fix | Delete
each_name(address) {|name| ret << name}
[254] Fix | Delete
return ret
[255] Fix | Delete
end
[256] Fix | Delete
[257] Fix | Delete
##
[258] Fix | Delete
# Iterates over all hostnames for +address+ retrieved from the hosts file.
[259] Fix | Delete
[260] Fix | Delete
def each_name(address, &proc)
[261] Fix | Delete
lazy_initialize
[262] Fix | Delete
if @addr2name.include?(address)
[263] Fix | Delete
@addr2name[address].each(&proc)
[264] Fix | Delete
end
[265] Fix | Delete
end
[266] Fix | Delete
end
[267] Fix | Delete
[268] Fix | Delete
##
[269] Fix | Delete
# Resolv::DNS is a DNS stub resolver.
[270] Fix | Delete
#
[271] Fix | Delete
# Information taken from the following places:
[272] Fix | Delete
#
[273] Fix | Delete
# * STD0013
[274] Fix | Delete
# * RFC 1035
[275] Fix | Delete
# * ftp://ftp.isi.edu/in-notes/iana/assignments/dns-parameters
[276] Fix | Delete
# * etc.
[277] Fix | Delete
[278] Fix | Delete
class DNS
[279] Fix | Delete
[280] Fix | Delete
##
[281] Fix | Delete
# Default DNS Port
[282] Fix | Delete
[283] Fix | Delete
Port = 53
[284] Fix | Delete
[285] Fix | Delete
##
[286] Fix | Delete
# Default DNS UDP packet size
[287] Fix | Delete
[288] Fix | Delete
UDPSize = 512
[289] Fix | Delete
[290] Fix | Delete
##
[291] Fix | Delete
# Group of DNS resolver threads (obsolete)
[292] Fix | Delete
[293] Fix | Delete
DNSThreadGroup = ThreadGroup.new
[294] Fix | Delete
[295] Fix | Delete
##
[296] Fix | Delete
# Creates a new DNS resolver. See Resolv::DNS.new for argument details.
[297] Fix | Delete
#
[298] Fix | Delete
# Yields the created DNS resolver to the block, if given, otherwise
[299] Fix | Delete
# returns it.
[300] Fix | Delete
[301] Fix | Delete
def self.open(*args)
[302] Fix | Delete
dns = new(*args)
[303] Fix | Delete
return dns unless block_given?
[304] Fix | Delete
begin
[305] Fix | Delete
yield dns
[306] Fix | Delete
ensure
[307] Fix | Delete
dns.close
[308] Fix | Delete
end
[309] Fix | Delete
end
[310] Fix | Delete
[311] Fix | Delete
##
[312] Fix | Delete
# Creates a new DNS resolver.
[313] Fix | Delete
#
[314] Fix | Delete
# +config_info+ can be:
[315] Fix | Delete
#
[316] Fix | Delete
# nil:: Uses /etc/resolv.conf.
[317] Fix | Delete
# String:: Path to a file using /etc/resolv.conf's format.
[318] Fix | Delete
# Hash:: Must contain :nameserver, :search and :ndots keys.
[319] Fix | Delete
#
[320] Fix | Delete
# Example:
[321] Fix | Delete
#
[322] Fix | Delete
# Resolv::DNS.new(:nameserver => ['210.251.121.21'],
[323] Fix | Delete
# :search => ['ruby-lang.org'],
[324] Fix | Delete
# :ndots => 1)
[325] Fix | Delete
[326] Fix | Delete
def initialize(config_info=nil)
[327] Fix | Delete
@mutex = Mutex.new
[328] Fix | Delete
@config = Config.new(config_info)
[329] Fix | Delete
@initialized = nil
[330] Fix | Delete
end
[331] Fix | Delete
[332] Fix | Delete
def lazy_initialize # :nodoc:
[333] Fix | Delete
@mutex.synchronize {
[334] Fix | Delete
unless @initialized
[335] Fix | Delete
@config.lazy_initialize
[336] Fix | Delete
@initialized = true
[337] Fix | Delete
end
[338] Fix | Delete
}
[339] Fix | Delete
self
[340] Fix | Delete
end
[341] Fix | Delete
[342] Fix | Delete
##
[343] Fix | Delete
# Closes the DNS resolver.
[344] Fix | Delete
[345] Fix | Delete
def close
[346] Fix | Delete
@mutex.synchronize {
[347] Fix | Delete
if @initialized
[348] Fix | Delete
@initialized = false
[349] Fix | Delete
end
[350] Fix | Delete
}
[351] Fix | Delete
end
[352] Fix | Delete
[353] Fix | Delete
##
[354] Fix | Delete
# Gets the IP address of +name+ from the DNS resolver.
[355] Fix | Delete
#
[356] Fix | Delete
# +name+ can be a Resolv::DNS::Name or a String. Retrieved address will
[357] Fix | Delete
# be a Resolv::IPv4 or Resolv::IPv6
[358] Fix | Delete
[359] Fix | Delete
def getaddress(name)
[360] Fix | Delete
each_address(name) {|address| return address}
[361] Fix | Delete
raise ResolvError.new("DNS result has no information for #{name}")
[362] Fix | Delete
end
[363] Fix | Delete
[364] Fix | Delete
##
[365] Fix | Delete
# Gets all IP addresses for +name+ from the DNS resolver.
[366] Fix | Delete
#
[367] Fix | Delete
# +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
[368] Fix | Delete
# be a Resolv::IPv4 or Resolv::IPv6
[369] Fix | Delete
[370] Fix | Delete
def getaddresses(name)
[371] Fix | Delete
ret = []
[372] Fix | Delete
each_address(name) {|address| ret << address}
[373] Fix | Delete
return ret
[374] Fix | Delete
end
[375] Fix | Delete
[376] Fix | Delete
##
[377] Fix | Delete
# Iterates over all IP addresses for +name+ retrieved from the DNS
[378] Fix | Delete
# resolver.
[379] Fix | Delete
#
[380] Fix | Delete
# +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
[381] Fix | Delete
# be a Resolv::IPv4 or Resolv::IPv6
[382] Fix | Delete
[383] Fix | Delete
def each_address(name)
[384] Fix | Delete
each_resource(name, Resource::IN::A) {|resource| yield resource.address}
[385] Fix | Delete
each_resource(name, Resource::IN::AAAA) {|resource| yield resource.address}
[386] Fix | Delete
end
[387] Fix | Delete
[388] Fix | Delete
##
[389] Fix | Delete
# Gets the hostname for +address+ from the DNS resolver.
[390] Fix | Delete
#
[391] Fix | Delete
# +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
[392] Fix | Delete
# name will be a Resolv::DNS::Name.
[393] Fix | Delete
[394] Fix | Delete
def getname(address)
[395] Fix | Delete
each_name(address) {|name| return name}
[396] Fix | Delete
raise ResolvError.new("DNS result has no information for #{address}")
[397] Fix | Delete
end
[398] Fix | Delete
[399] Fix | Delete
##
[400] Fix | Delete
# Gets all hostnames for +address+ from the DNS resolver.
[401] Fix | Delete
#
[402] Fix | Delete
# +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
[403] Fix | Delete
# names will be Resolv::DNS::Name instances.
[404] Fix | Delete
[405] Fix | Delete
def getnames(address)
[406] Fix | Delete
ret = []
[407] Fix | Delete
each_name(address) {|name| ret << name}
[408] Fix | Delete
return ret
[409] Fix | Delete
end
[410] Fix | Delete
[411] Fix | Delete
##
[412] Fix | Delete
# Iterates over all hostnames for +address+ retrieved from the DNS
[413] Fix | Delete
# resolver.
[414] Fix | Delete
#
[415] Fix | Delete
# +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
[416] Fix | Delete
# names will be Resolv::DNS::Name instances.
[417] Fix | Delete
[418] Fix | Delete
def each_name(address)
[419] Fix | Delete
case address
[420] Fix | Delete
when Name
[421] Fix | Delete
ptr = address
[422] Fix | Delete
when IPv4::Regex
[423] Fix | Delete
ptr = IPv4.create(address).to_name
[424] Fix | Delete
when IPv6::Regex
[425] Fix | Delete
ptr = IPv6.create(address).to_name
[426] Fix | Delete
else
[427] Fix | Delete
raise ResolvError.new("cannot interpret as address: #{address}")
[428] Fix | Delete
end
[429] Fix | Delete
each_resource(ptr, Resource::IN::PTR) {|resource| yield resource.name}
[430] Fix | Delete
end
[431] Fix | Delete
[432] Fix | Delete
##
[433] Fix | Delete
# Look up the +typeclass+ DNS resource of +name+.
[434] Fix | Delete
#
[435] Fix | Delete
# +name+ must be a Resolv::DNS::Name or a String.
[436] Fix | Delete
#
[437] Fix | Delete
# +typeclass+ should be one of the following:
[438] Fix | Delete
#
[439] Fix | Delete
# * Resolv::DNS::Resource::IN::A
[440] Fix | Delete
# * Resolv::DNS::Resource::IN::AAAA
[441] Fix | Delete
# * Resolv::DNS::Resource::IN::ANY
[442] Fix | Delete
# * Resolv::DNS::Resource::IN::CNAME
[443] Fix | Delete
# * Resolv::DNS::Resource::IN::HINFO
[444] Fix | Delete
# * Resolv::DNS::Resource::IN::MINFO
[445] Fix | Delete
# * Resolv::DNS::Resource::IN::MX
[446] Fix | Delete
# * Resolv::DNS::Resource::IN::NS
[447] Fix | Delete
# * Resolv::DNS::Resource::IN::PTR
[448] Fix | Delete
# * Resolv::DNS::Resource::IN::SOA
[449] Fix | Delete
# * Resolv::DNS::Resource::IN::TXT
[450] Fix | Delete
# * Resolv::DNS::Resource::IN::WKS
[451] Fix | Delete
#
[452] Fix | Delete
# Returned resource is represented as a Resolv::DNS::Resource instance,
[453] Fix | Delete
# i.e. Resolv::DNS::Resource::IN::A.
[454] Fix | Delete
[455] Fix | Delete
def getresource(name, typeclass)
[456] Fix | Delete
each_resource(name, typeclass) {|resource| return resource}
[457] Fix | Delete
raise ResolvError.new("DNS result has no information for #{name}")
[458] Fix | Delete
end
[459] Fix | Delete
[460] Fix | Delete
##
[461] Fix | Delete
# Looks up all +typeclass+ DNS resources for +name+. See #getresource for
[462] Fix | Delete
# argument details.
[463] Fix | Delete
[464] Fix | Delete
def getresources(name, typeclass)
[465] Fix | Delete
ret = []
[466] Fix | Delete
each_resource(name, typeclass) {|resource| ret << resource}
[467] Fix | Delete
return ret
[468] Fix | Delete
end
[469] Fix | Delete
[470] Fix | Delete
##
[471] Fix | Delete
# Iterates over all +typeclass+ DNS resources for +name+. See
[472] Fix | Delete
# #getresource for argument details.
[473] Fix | Delete
[474] Fix | Delete
def each_resource(name, typeclass, &proc)
[475] Fix | Delete
lazy_initialize
[476] Fix | Delete
requester = make_requester
[477] Fix | Delete
senders = {}
[478] Fix | Delete
begin
[479] Fix | Delete
@config.resolv(name) {|candidate, tout, nameserver|
[480] Fix | Delete
msg = Message.new
[481] Fix | Delete
msg.rd = 1
[482] Fix | Delete
msg.add_question(candidate, typeclass)
[483] Fix | Delete
unless sender = senders[[candidate, nameserver]]
[484] Fix | Delete
sender = senders[[candidate, nameserver]] =
[485] Fix | Delete
requester.sender(msg, candidate, nameserver)
[486] Fix | Delete
end
[487] Fix | Delete
reply, reply_name = requester.request(sender, tout)
[488] Fix | Delete
case reply.rcode
[489] Fix | Delete
when RCode::NoError
[490] Fix | Delete
extract_resources(reply, reply_name, typeclass, &proc)
[491] Fix | Delete
return
[492] Fix | Delete
when RCode::NXDomain
[493] Fix | Delete
raise Config::NXDomain.new(reply_name.to_s)
[494] Fix | Delete
else
[495] Fix | Delete
raise Config::OtherResolvError.new(reply_name.to_s)
[496] Fix | Delete
end
[497] Fix | Delete
}
[498] Fix | Delete
ensure
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function