From f621e2ba2b86660f4fbc735c38287569c1704ab3 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 14 May 2012 00:38:25 +0200 Subject: More error handling --- mdns-resolver | 80 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/mdns-resolver b/mdns-resolver index 2cc2ece..5cefc25 100755 --- a/mdns-resolver +++ b/mdns-resolver @@ -75,64 +75,66 @@ while run do query = Net::DNS::Packet::parse(data) - return unless query.header.opCode == Net::DNS::Header::QUERY return unless query.header.query? packet = Net::DNS::Packet.new packet.header.id = query.header.id - packet.header.opCode = Net::DNS::Header::QUERY + packet.header.opCode = query.header.opCode packet.header.qr = 1 packet.header.aa = 1 packet.header.rd = query.header.recursive? + query.question.each { |q| packet.question << q } - query.question.each do |q| - packet.question << q + if query.header.opCode == Net::DNS::Header::QUERY + query.question.each do |q| + next unless q.qClass.to_i == Net::DNS::IN - next unless q.qClass.to_i == Net::DNS::IN + if q.qName == $domain+'.' + packet.answer << soaRecord if q.qType.to_i == Net::DNS::SOA or q.qType.to_i == Net::DNS::ANY + $ns.each { |line| packet.answer << Net::DNS::RR::NS.new(line) } if q.qType.to_i == Net::DNS::NS or q.qType.to_i == Net::DNS::ANY + else + host, qdomain = q.qName.split('.', 2) + next unless qdomain == $domain+'.' - if q.qName == $domain+'.' - packet.answer << soaRecord if q.qType.to_i == Net::DNS::SOA or q.qType.to_i == Net::DNS::ANY - $ns.each { |line| packet.answer << Net::DNS::RR::NS.new(line) } if q.qType.to_i == Net::DNS::NS or q.qType.to_i == Net::DNS::ANY - else - host, qdomain = q.qName.split('.', 2) - next unless qdomain == $domain+'.' - - begin - addresses = Set.new begin - Socket.getaddrinfo(host+'.local.', nil, :INET).each { |addr| addresses << [addr[0], addr[3]] } - rescue - end + addresses = Set.new + begin + Socket.getaddrinfo(host+'.local.', nil, :INET).each { |addr| addresses << [addr[0], addr[3]] } + rescue + end - begin - Socket.getaddrinfo(host+'.local.', nil, :INET6).each { |addr| addresses << [addr[0], addr[3]] } - rescue - end + begin + Socket.getaddrinfo(host+'.local.', nil, :INET6).each { |addr| addresses << [addr[0], addr[3]] } + rescue + end - raise if addresses.empty? + raise if addresses.empty? - if q.qType.to_i == Net::DNS::A or q.qType.to_i == Net::DNS::ANY - addresses.each do |addr| - packet.answer << Net::DNS::RR::A.new(:name => q.qName, - :ttl => $ttl, - :cls => Net::DNS::IN, - :type => Net::DNS::A, - :address => addr[1]) if addr[0] == "AF_INET" + if q.qType.to_i == Net::DNS::A or q.qType.to_i == Net::DNS::ANY + addresses.each do |addr| + packet.answer << Net::DNS::RR::A.new(:name => q.qName, + :ttl => $ttl, + :cls => Net::DNS::IN, + :type => Net::DNS::A, + :address => addr[1]) if addr[0] == "AF_INET" + end end - end - if q.qType.to_i == Net::DNS::AAAA or q.qType.to_i == Net::DNS::ANY - addresses.each do |addr| - packet.answer << Net::DNS::RR::AAAA.new(:name => q.qName, - :ttl => $ttl, - :cls => Net::DNS::IN, - :type => Net::DNS::AAAA, - :address => addr[1]) if addr[0] == "AF_INET6" + if q.qType.to_i == Net::DNS::AAAA or q.qType.to_i == Net::DNS::ANY + addresses.each do |addr| + packet.answer << Net::DNS::RR::AAAA.new(:name => q.qName, + :ttl => $ttl, + :cls => Net::DNS::IN, + :type => Net::DNS::AAAA, + :address => addr[1]) if addr[0] == "AF_INET6" + end end + rescue + packet.header.rCode = Net::DNS::Header::RCode::NAME end - rescue - packet.header.rCode = Net::DNS::Header::RCode::NAME end end + else + packet.header.rCode = Net::DNS::Header::RCode::NOTIMPLEMENTED end if packet.answer.empty? -- cgit v1.2.3