Class: PacketGen::Header::DNS

Inherits:
Base
  • Object
show all
Defined in:
lib/packetgen/header/dns.rb,
lib/packetgen/header/dns.rb,
lib/packetgen/header/dns/rr.rb,
lib/packetgen/header/dns/opt.rb,
lib/packetgen/header/dns/name.rb,
lib/packetgen/header/dns/option.rb,
lib/packetgen/header/dns/question.rb,
lib/packetgen/header/dns/qdsection.rb,
lib/packetgen/header/dns/rrsection.rb

Overview

DNS: Domain Name Service

A DNS packet consists of a header:

                                1  1  1  1  1  1
  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                      ID                       |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    QDCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ANCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    NSCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ARCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

A DNS packet also contains up to 4 sections:

  • #qd, question section,

  • #an, answer section,

  • #ns, authoritary section,

  • #ar, additional information section.

Examples:

Create a DNS header

# standalone
dns = PacketGen::Header::DNS.new
# in a IP packet
pkt = PacketGen.gen('IP').add('UDP').add('DNS')
# access to DNS header
pkt.dns.class   # => PacketGen::Header::DNS

DNS attributes

dns = PacketGen::Header::DNS.new
dns.id = 0x1234
dns.qr = false
# opcode may be set as an Integer (all values are possible)
# or as a String (only keys from PacketGen::Header::DNS::OPCODES)
dns.opcode = 0xe       # set as integer, value not defined in standard
dns.opcode = 'query'   # set as string
# Set flags
dns.aa = dns.tc = dns.rd = dns.ra = false
# rcode may be set as an Integer (all values are possible)
# or as a String (only keys from PacketGen::Header::DNS::RCODES)
dns.rcode = 11
dns.rcode = 'refused'
# Section counts may be set by hand. They also be set automatically when adding
# an element to a section.
dns.qdcount = 123
dns.ancount = 0x1234
dns.nscount = 1
dns.arcount = 0
# Access to DNS sections:
dns.qd.class   # => PacketGen::Header::DNS::QDSection
dns.an.class   # => PacketGen::Header::DNS::RRSection
dns.ns.class   # => PacketGen::Header::DNS::RRSection
dns.ar.class   # => PacketGen::Header::DNS::RRSection

Add a question to DNS question section

dns = PacketGen::Header::DNS.new
# add a question about example.net IP address. Increment qdcount
dns.qd << PacketGen::Header::DNS::Question.new(dns, name: 'example.net')
# or
dns.qd << { rtype: 'Question', name: 'example.net' }
dns.qdcount    #=> 2
# add a question about example.net IPv6 address. Dot not modify qdcount
dns.qd.push(PacketGen::Header::DNS::Question.new(dns, name: 'example.net', type: 'AAAA'))
# or
dns.qd.push(rtype: 'Question', name: 'example.net', type: 'AAAA')
dns.qdcount    #=> 2

Add a ressource record to a DNS section

dns = PacketGen::Header::DNS.new
# add a RR to answer section. Increment ancount
dns.an << PacketGen::Header::DNS::RR.new(dns, name: 'example.net', rdata: IPAddr.new('1.2.3.4').hton)
# or
dns.an << { rtype: 'RR', name: 'example.net', rdata: IPAddr.new('1.2.3.4').hton }
dns.ancount   #=> 2
# add a RR to NS section. Dot not modify nscount
rdata = PacketGen::Header::DNS::Name.new(dns: dns).from_human('dns.net')
dns.ns.push(PacketGen::Header::DNS::RR.new(dns, name: 'example.net', type: 'NS', rdata: rdata))
# or
dns.ns.push(rtype: 'RR', name: 'example.net', type: 'NS', rdata: rdata)
dns.nscount   #=> 0

Extended DNS (EDNS(0)) options

dns = PacketGen::Header::DNS.new
# Add an OPT to ar section
dns.ar << PacketGen::Header::DNS::OPT.new(dns, udp_size: 4096, ext_rcode: 43)
# or
dns.ar << { rtype: 'OPT', udp_size: 4096, ext_rcode: 43 }
# add an option to OPT record
dns.ar.last.options << PacketGen::Header::DNS::Option.new(code: 48, data: '12')
# or
dns.ar.last.options << { code: 48, data: '12' }

Author:

  • Sylvain Daubert

Since:

  • 1.3.0

Direct Known Subclasses

MDNS

Defined Under Namespace

Classes: ArrayOfOptions, Name, OPT, Option, QDSection, Question, RR, RRSection

Constant Summary collapse

UDP_PORT =

Port number for DNS over UDP

Since:

  • 1.3.0

53
TCP_PORT =

Port number for DNS over TCP

Since:

  • 1.3.0

UDP_PORT
OPCODES =

DNS opcodes

Since:

  • 1.3.0

{
  'query' => 0,
  'iquery' => 1,
  'status' => 2,
  'notify' => 4,
  'update' => 5
}.freeze
RCODES =

DNS Response codes

Since:

  • 1.3.0

{
  'ok' => 0,
  'no-error' => 0,
  'format-error' => 1,
  'server-failure' => 2,
  'name-error' => 3,
  'not-implemented' => 4,
  'refused' => 5
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

bind, calculate_and_set_length, #header_id, inherited, #initialize, #ip_header, #ll_header

Methods included from PacketGen::Headerable

#added_to_packet, included, #method_name, #packet, #packet=, #parse?, #protocol_name, #read, #to_s

Constructor Details

This class inherits a constructor from PacketGen::Header::Base

Instance Attribute Details

#aaBoolean

Returns Authoritative answer.

Returns:

  • (Boolean)

    Authoritative answer


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#adBoolean

Returns Authentic Data.

Returns:

  • (Boolean)

    Authentic Data


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#anRRSection

Answer section

Returns:


199
# File 'lib/packetgen/header/dns.rb', line 199

define_attr :an, RRSection, builder: ->(h, t) { t.new(h, h[:ancount]) }

#ancountInteger

Number of ressource recors in answer section

Returns:

  • (Integer)

183
# File 'lib/packetgen/header/dns.rb', line 183

define_attr :ancount, BinStruct::Int16

#arRRSection

Additional record section

Returns:


207
# File 'lib/packetgen/header/dns.rb', line 207

define_attr :ar, RRSection, builder: ->(h, t) { t.new(h, h[:arcount]) }

#arcountInteger

Number of ressources recors in additional records section

Returns:

  • (Integer)

191
# File 'lib/packetgen/header/dns.rb', line 191

define_attr :arcount, BinStruct::Int16

#cdBoolean

Returns Checking Disabled.

Returns:

  • (Boolean)

    Checking Disabled


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#idInteger

16-bit identifier to match up replies and queries

Returns:

  • (Integer)

152
# File 'lib/packetgen/header/dns.rb', line 152

define_attr :id, BinStruct::Int16

#nsRRSection

Authority records section

Returns:


203
# File 'lib/packetgen/header/dns.rb', line 203

define_attr :ns, RRSection, builder: ->(h, t) { t.new(h, h[:nscount]) }

#nscountInteger

Number of name resource recors in authority records section

Returns:

  • (Integer)

187
# File 'lib/packetgen/header/dns.rb', line 187

define_attr :nscount, BinStruct::Int16

#opcodeInteger

Returns Kind of query. See OPCODES.

Returns:

  • (Integer)

    Kind of query. See OPCODES.


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#qdQDSection

Question section

Returns:


195
# File 'lib/packetgen/header/dns.rb', line 195

define_attr :qd, QDSection, builder: ->(h, t) { t.new(h, h[:qdcount]) }

#qdcountInteger

Number of entries in question section

Returns:

  • (Integer)

179
# File 'lib/packetgen/header/dns.rb', line 179

define_attr :qdcount, BinStruct::Int16

#qrBoolean

Returns query (false) or response (true).

Returns:

  • (Boolean)

    query (false) or response (true)


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#raBoolean

Returns Recursion Available.

Returns:

  • (Boolean)

    Recursion Available


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#rcodeInteger

Returns Response code. See RCODES.

Returns:

  • (Integer)

    Response code. See RCODES.


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#rdBoolean

Returns Recursion Desired.

Returns:

  • (Boolean)

    Recursion Desired


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#tcBoolean

Returns Truncation.

Returns:

  • (Boolean)

    Truncation


173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

#u16Integer

Returns:

  • (Integer)

173
# File 'lib/packetgen/header/dns.rb', line 173

define_bit_attr :u16, qr: 1, opcode: 4, aa: 1, tc: 1, rd: 1, ra: 1, z: 1, ad: 1, cd: 1, rcode: 4

Instance Method Details

#inspectString

Returns:

  • (String)

Since:

  • 1.3.0


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/packetgen/header/dns.rb', line 254

def inspect
  super do |attr|
    next unless attr == :u16

    str = inspect_flags

    str << Inspect.shift_level
    opcode = '%-16s (%u)' % [OPCODES.key(self.opcode), self.opcode]
    str << Inspect::FMT_ATTR % ['Integer', 'opcode', opcode]

    str << Inspect.shift_level
    rcode = '%-16s (%u)' % [RCODES.key(self.rcode), self.rcode]
    str << Inspect::FMT_ATTR % ['Integer', 'rcode', rcode]
  end
end

#query?Boolean

Is message a query

Returns:

  • (Boolean)

Since:

  • 1.3.0


249
250
251
# File 'lib/packetgen/header/dns.rb', line 249

def query?
  !qr?
end

#response?Boolean

Is message a response

Returns:

  • (Boolean)

Since:

  • 1.3.0


243
244
245
# File 'lib/packetgen/header/dns.rb', line 243

def response?
  qr?
end