1
0
mirror of https://code.semirocket.science/wrapsix synced 2024-11-10 00:01:01 +02:00

Resolver is working but not for normal apps yet(?)

Accepts requests and sends responses
This commit is contained in:
xHire 2008-11-26 23:44:06 +01:00
parent f09255b0d7
commit c27f79705f
2 changed files with 40 additions and 32 deletions

View File

@ -1,6 +1,7 @@
### Configuration of Resolver
resolver: 1
resolver_ip: '::1'
#resolver_ip: '::1'
resolver_ip: '2001:470:9985:1:200:e2ff:fe7f:414'
secondary_resolver: 10.0.0.139 # if not set => /etc/resolv.conf
### Configuration of Wrapper

View File

@ -19,20 +19,17 @@ class Resolver
def start
@resolver = UDPSocket.open Socket::AF_INET6
if @resolver.bind $config['resolver_ip'], 53
p "Started DNS resolver on IPv6 address #{$config['resolver_ip']}" if @debug
puts "Started DNS resolver on IPv6 address #{$config['resolver_ip']}" if @debug
else
p "DNS resolver not started!" if @debug
puts "DNS resolver not started!" if @debug
end
# just for now
#@hosts = [
#{:name => "example.com", :type => "A", :data => "192.168.0.1"}
#]
loop do
puts "---------- start ----------" if @debug
# Receive and parse query
data = @resolver.recvfrom 2048
p "Client: #{data[1].join(' ')}" if @debug
print "Client: " if @debug
p data[1] if @debug
query = Resolv::DNS::Message::decode data[0]
print "Whole query: " if @debug
@ -44,36 +41,46 @@ class Resolver
answer.opcode = query.opcode # Type of Query; copy from query
answer.aa = 0 # Is this an authoritative response: 0 = No, 1 = Yes
answer.rd = query.rd # Is Recursion Desired, copied from query
answer.ra = 1 # Does name server support recursion: 0 = No, 1 = Yes
answer.ra = 0 # Does name server support recursion: 0 = No, 1 = Yes
answer.rcode = 0 # Response code: 0 = No errors
query.each_question do |question, typeclass| # There may be multiple questions per query
name = question.to_s # The domain name looked for in the query.
p "Looking for: #{name}" if @debug
record_type = typeclass.name.split("::").last # For example "A", "MX"
p "RR: #{typeclass}" if @debug
p "RR: #{record_type}" if @debug
begin
name = question.to_s # The domain name looked for in the query.
puts "Looking for: #{name}" if @debug
#record_type = typeclass.name.split("::").last # For example "A", "MX"
puts "RR: #{typeclass}" if @debug
#puts "RR: #{record_type}" if @debug
# So let's look for it :c) (in secondary resolver)
sr = Resolv::DNS::new :nameserver => $config['secondary_resolver']
sr_data = sr.getresource name, typeclass
sr_answer = sr_data.address # this is acceptable only for A or so
p sr_answer if @debug
# So let's look for it :c) (in secondary resolver)
sr = Resolv::DNS::new :nameserver => $config['secondary_resolver']
sr_data = sr.getresource name, typeclass
print "Raw answer: " if @debug
p sr_data if @debug
# temporary code
#ttl = 16000
#ttl = 86400 # 1 day
#record = @hosts.find{|host| host[:name] == name && host[:type] == record_type }
#unless record.nil?
# Setup answer to this question
#answer.add_answer(name + ".",ttl,typeclass.new(record[:data]))
#answer.encode
#end
record = {}
record[:name] = name
record[:data] = sr_data
print "Data: " if @debug
p record if @debug
# completing the answer
ttl = 86400 # I think ttl doesn't matter ;c)
answer.add_answer name + ".", ttl, record[:data]
print "My answer: " if @debug
p answer if @debug
rescue Resolv::ResolvError
puts "Error: DNS result has no information for #{name}"
end
end
end
# Send the response
#server.send answer.encode, 0, data[1][2], data[1][1]
# send the response
@resolver.send answer.encode, 0, data[1][3], data[1][1] # msg, flags, client, port
puts "---------- end ----------" if @debug
end
end
def exit