Sha256: 61baa8a80851e1acd216c94470de0dea69b2c72f383ec24b06e6b1d699ded497

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

require 'net/dns/packet'

module DomainUtil

  def DomainUtil.parse_domain_request(data=[])
    return unless data && !data.empty?
    data = data.split(/\r?\n/) unless data.kind_of? Array
    data.shift while (data[0].empty? || data[0].nil?)   # Remove leading empties
    
    dns_data = Net::DNS::Packet.parse( data.join($/) )
    return unless dns_data.header.query? 
    domain_name = dns_data.question[0].qName
    return dns_data, domain_name, dns_data.header
  end
  
  def DomainUtil.parse_domain_response(data=[])
    return unless data && !data.empty?
    data = data.split(/\r?\n/) unless data.kind_of? Array
    data.shift while (data[0].empty? || data[0].nil?)   # Remove leading empties
    
    # This is the heavy lifting. 
    dns_data = Net::DNS::Packet.parse( data.join($/) )
    
    # We don't want queries or empty answers
    return if dns_data.header.query? || dns_data.answer.nil? || dns_data.answer.empty?
    
    domain_name = dns_data.answer[0].name
    
    # Empty the lists if they are already populated
    addresses = []
    cnames = []

    # Store the CNAMEs associated to this domain. Can be empty. 
    dns_data.each_cname do |cname|
      cnames << cname.to_s
    end

    # Store the IP address for this domain. If empty, the lookup was unsuccessful. 
    dns_data.each_address do |ip|
      addresses << ip.to_s
    end
    
    return dns_data, domain_name, dns_data.header, addresses, cnames
  end

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
solutious-stella-0.5.5 lib/utils/domainutil.rb
stella-0.5.5 lib/utils/domainutil.rb
stella-0.5.4 lib/utils/domainutil.rb