summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-05-14 00:38:25 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-05-14 00:38:25 +0200
commitf621e2ba2b86660f4fbc735c38287569c1704ab3 (patch)
tree14ed11fbb6bc34490d811cbea21e55660b618a34
parent72b525e9d29735ab154208676ad6b6f33c539884 (diff)
downloadmdns-resolver-master.tar
mdns-resolver-master.zip
More error handlingHEADmaster
-rwxr-xr-xmdns-resolver80
1 files 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?