From c27f79705ff120f54a0067905406b1df9c2991cf Mon Sep 17 00:00:00 2001 From: xHire Date: Wed, 26 Nov 2008 23:44:06 +0100 Subject: [PATCH] Resolver is working but not for normal apps yet(?) Accepts requests and sends responses --- conf/wrapsix.conf | 3 ++- lib/resolver.rb | 69 ++++++++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/conf/wrapsix.conf b/conf/wrapsix.conf index f6d9e5e..6ccbc02 100644 --- a/conf/wrapsix.conf +++ b/conf/wrapsix.conf @@ -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 diff --git a/lib/resolver.rb b/lib/resolver.rb index 10a3154..8459fd5 100644 --- a/lib/resolver.rb +++ b/lib/resolver.rb @@ -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