Class Net::DNS::Packet
In: lib/net/dns/packet.rb
Parent: Object

Net::DNS::Packet

The Net::DNS::Packet class represents an entire DNS packet, divided in his main section:

You can use this class whenever you need to create a DNS packet, whether in an user application, in a resolver instance (have a look, for instance, at the Net::DNS::Resolver#send method) or for a nameserver.

For example:

  # Create a packet
  packet = Net::DNS::Packet.new("www.example.com")
  mx = Net::DNS::Packet.new("example.com", Net::DNS::MX)

  # Getting packet binary data, suitable for network transmission
  data = packet.data

A packet object can be created from binary data too, like an answer packet just received from a network stream:

  packet = Net::DNS::Packet::parse(data)

Each part of a packet can be gotten by the right accessors:

  header = packet.header     # Instance of Net::DNS::Header class
  question = packet.question # Instance of Net::DNS::Question class

  # Iterate over additional RRs
  packet.additional.each do |rr|
    puts "Got an #{rr.type} record"
  end

Some iterators have been written to easy the access of those RRs, which are often the most important. So instead of doing:

  packet.answer.each do |rr|
    if rr.type == Net::DNS::RR::Types::A
      # do something with +rr.address+
    end
  end

we can do:

  packet.each_address do |ip|
    # do something with +ip+
  end

Be sure you don‘t miss all the iterators in the class documentation.

Logging facility

As Net::DNS::Resolver class, Net::DNS::Packet class has its own logging facility too. It work in the same way the other one do, so you can maybe want to override it or change the file descriptor.

  packet = Net::DNS::Packet.new("www.example.com")
  packet.logger = $stderr

  # or even
  packet.logger = Logger.new("/tmp/packet.log")

If the Net::DNS::Packet class is directly instantiated by the Net::DNS::Resolver class, like the great majority of the time, it will use the same logger facility.

Logger level will be set to Logger::Debug if $DEBUG variable is set.

Methods

Included Modules

Names

Classes and Modules

Class Net::DNS::Packet::Error
Class Net::DNS::Packet::PacketError

Attributes

additional  [R] 
answer  [R] 
answerfrom  [R] 
answersize  [R] 
authority  [R] 
header  [R] 
question  [R] 

Public Class methods

Creates a new instance of Net::DNS::Packet class. Arguments are the canonical name of the resource, an optional type field and an optional class field. The record type and class can be omitted; they default to A and IN.

  packet = Net::DNS::Packet.new("www.example.com")
  packet = Net::DNS::Packet.new("example.com", Net::DNS::MX)
  packet = Net::DNS::Packet.new("example.com", Net::DNS::TXT, Net::DNS::CH)

This class no longer instantiate object from binary data coming from network streams. Please use Net::DNS::Packet.parse instead.

Creates a new instance of Net::DNS::Packet class from binary data, taken out from a network stream. For example:

  # udp_socket is an UDPSocket waiting for a response
  ans = udp_socket.recvfrom(1500)
  packet = Net::DNS::Packet::parse(ans)

An optional from argument can be used to specify the information of the sender. If data is passed as is from a Socket#recvfrom call, the method will accept it.

Be sure that your network data is clean from any UDP/TCP header, especially when using RAW sockets.

Public Instance methods

Assigns one or an array of Net::DNS::RR objects to the additional section of this Net::DNS::Packet instance.

Assigns one or an array of Net::DNS::RR objects to the answer section of this Net::DNS::Packet instance.

Assigns one or an array of Net::DNS::RR objects to the authority section of this Net::DNS::Packet instance.

Returns the packet object in binary data, suitable for sending across a network stream.

  packet_data = packet.data
  puts "Packet is #{packet_data.size} bytes long"

Same as Net::DNS::Packet#data, but implements name compression (see RFC1025) for a considerable save of bytes.

  packet = Net::DNS::Packet.new("www.example.com")
  puts "Size normal is #{packet.data.size} bytes"
  puts "Size compressed is #{packet.data_comp.size} bytes"

Iterates every address in the answer section of this Net::DNS::Packet instance.

  packet.each_address do |ip|
    ping ip.to_s
  end

As you can see in the documentation for the Net::DNS::RR::A class, the address returned is an instance of IPAddr class.

Iterates every canonical name in the answer section of this Net::DNS::Packet instance.

  packet.each_cname do |cname|
    puts "Canonical name: #{cname}"
  end

Iterates every exchange record in the answer section of this Net::DNS::Packet instance.

  packet.each_mx do |pref,name|
    puts "Mail exchange #{name} has preference #{pref}"
  end

Iterates every nameserver in the answer section of this Net::DNS::Packet instance.

  packet.each_nameserver do |ns|
    puts "Nameserver found: #{ns}"
  end

Iterates every pointer in the answer section of this Net::DNS::Packet instance.

  packet.each_ptr do |ptr|
    puts "Pointer for resource: #{ptr}"
  end

Assigns a Net::DNS::Header object to this Net::DNS::Packet instance.

Returns a string containing a human-readable representation of this Net::DNS::Packet instance.

Checks whether the query returned a NXDOMAIN error, meaning the queried domain name doesn‘t exist.

  %w[a.com google.com ibm.com d.com].each do |domain|
    response = Net::DNS::Resolver.new.send(domain)
    puts "#{domain} doesn't exist" if response.nxdomain?
  end
  # => a.com doesn't exist
  # => d.com doesn't exist

Checks if the packet is a QUERY packet

Assigns a Net::DNS::Question object to this Net::DNS::Packet instance.

Returns the packet size in bytes.

  Resolver("www.google.com") do |packet|
    puts packet.size + " bytes"}
  end
  # => 484 bytes
to_s()

Alias for inspect

Delegates to Net::DNS::Header#truncated?.

[Validate]