diff options
-rwxr-xr-x | mdns-resolver | 124 |
1 files changed, 62 insertions, 62 deletions
diff --git a/mdns-resolver b/mdns-resolver index 78d0169..e00d68c 100755 --- a/mdns-resolver +++ b/mdns-resolver @@ -51,76 +51,76 @@ sock.bind('::', port) while run do data, sendaddr, rflags, *controls = sock.recvmsg - #Thread.new(data, sendaddr, controls) do |data, sendaddr, controls| - pktinfo = nil - controls.each do |c| - pktinfo = c if c.cmsg_is?(:IP, :PKTINFO) or c.cmsg_is?(:IPV6, :PKTINFO) - end - return if pktinfo.nil? + Thread.new(data, sendaddr, controls) do |data, sendaddr, controls| + pktinfo = nil + controls.each do |c| + pktinfo = c if c.cmsg_is?(:IP, :PKTINFO) or c.cmsg_is?(:IPV6, :PKTINFO) + end + return if pktinfo.nil? - packet = Net::DNS::Packet::parse(data) + packet = Net::DNS::Packet::parse(data) - return unless packet.header.opCode == Net::DNS::Header::QUERY - return unless packet.header.query? + return unless packet.header.opCode == Net::DNS::Header::QUERY + return unless packet.header.query? - packet.header.qr = 1 - packet.header.aa = 1 - packet.header.tc = 0 - packet.header.ra = 0 - packet.header.cd = 0 - packet.header.ad = 0 - packet.header.rCode = Net::DNS::Header::RCode::NOERROR + packet.header.qr = 1 + packet.header.aa = 1 + packet.header.tc = 0 + packet.header.ra = 0 + packet.header.cd = 0 + packet.header.ad = 0 + packet.header.rCode = Net::DNS::Header::RCode::NOERROR - packet.question.each do |q| - next unless q.qClass.to_i == Net::DNS::IN + packet.question.each do |q| + next unless q.qClass.to_i == Net::DNS::IN - host, qdomain = q.qName.split('.', 2) - next unless qdomain == domain+'.' - - begin - if q.qType.to_i == Net::DNS::A - addresses = Set.new - Socket.getaddrinfo(host+'.local.', nil, :INET).each { |addr| addresses << addr[3] } - - 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) - end - elsif q.qType.to_i == Net::DNS::AAAA - addresses = Set.new - Socket.getaddrinfo(host+'.local.', nil, :INET6).each { |addr| addresses << addr[3] } - - 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) + host, qdomain = q.qName.split('.', 2) + next unless qdomain == domain+'.' + + begin + if q.qType.to_i == Net::DNS::A + addresses = Set.new + Socket.getaddrinfo(host+'.local.', nil, :INET).each { |addr| addresses << addr[3] } + + 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) + end + elsif q.qType.to_i == Net::DNS::AAAA + addresses = Set.new + Socket.getaddrinfo(host+'.local.', nil, :INET6).each { |addr| addresses << addr[3] } + + 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) + end end + rescue + packet.header.rCode = Net::DNS::Header::RCode::NAME end - rescue - packet.header.rCode = Net::DNS::Header::RCode::NAME end - end - if packet.answer.empty? - record = Net::DNS::RR::SOA.new(soa) - packet.authority = [Net::DNS::RR::SOA.new(:name => record.name, - :ttl => record.ttl, - :mname => record.mname, - :rname => record.rname, - :refresh => record.refresh, - :retry => record.retry, - :expire => record.expire, - :minimum => record.minimum, - :serial => Time.now.to_i)] - else - ns.each { |line| packet.authority << Net::DNS::RR::NS.new(line) } - end + if packet.answer.empty? + record = Net::DNS::RR::SOA.new(soa) + packet.authority = [Net::DNS::RR::SOA.new(:name => record.name, + :ttl => record.ttl, + :mname => record.mname, + :rname => record.rname, + :refresh => record.refresh, + :retry => record.retry, + :expire => record.expire, + :minimum => record.minimum, + :serial => Time.now.to_i)] + else + ns.each { |line| packet.authority << Net::DNS::RR::NS.new(line) } + end - sock.sendmsg(packet.data, 0, sendaddr, pktinfo) - #end + sock.sendmsg(packet.data, 0, sendaddr, pktinfo) + end end |