Class: PacketGen::Header::DNS::RR

Inherits:
Question show all
Defined in:
lib/packetgen/header/dns/rr.rb

Overview

DNS Ressource Record

Author:

  • Sylvain Daubert

Since:

  • 1.3.0

Direct Known Subclasses

OPT

Constant Summary

Constants inherited from Question

Question::CLASSES, Question::TYPES

Instance Attribute Summary collapse

Attributes inherited from Question

#name, #rrclass, #type

Instance Method Summary collapse

Methods inherited from Question

#human_type, #type?

Methods included from Types::Fieldable

#format_inspect, #read, #sz, #to_s, #type_name

Methods inherited from Types::Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, fields, #fields, inherited, #inspect, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

Constructor Details

#initialize(dns, options = {}) ⇒ RR

Returns a new instance of RR.

Parameters:

  • dns (DNS)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)

    domain as a dotted string

  • :type (Integer, String)

    see Question::TYPES. Default to 'A'

  • :rrclass (Integer, String)

    see Question::CLASSES. Default to 'IN'

  • :ttl (Integer)
  • :rdlength (Integer)

    if not provided, automatically set from :rdata length

  • :rdata (String)

Since:

  • 1.3.0


36
37
38
39
40
41
# File 'lib/packetgen/header/dns/rr.rb', line 36

def initialize(dns, options={})
  super
  return unless options[:rdata] && options[:rdlength].nil?

  self.rdata = options[:rdata]
end

Instance Attribute Details

#rdataTypes::String

Returns:


24
25
# File 'lib/packetgen/header/dns/rr.rb', line 24

define_field :rdata, Types::String,
builder: ->(rr, t) { t.new(length_from: rr[:rdlength]) }

#rdlengthInteger

16-bit #rdata length

Returns:

  • (Integer)

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

define_field :rdlength, Types::Int16

#ttlInteger

32-bit time to live

Returns:

  • (Integer)

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

define_field :ttl, Types::Int32

Instance Method Details

#human_rdataString

Get human readable rdata

Returns:

  • (String)

Since:

  • 1.3.0


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/packetgen/header/dns/rr.rb', line 57

def human_rdata
  str = human_ip_rdata || self[:rdata].inspect

  case type
  when TYPES['NS'], TYPES['PTR'], TYPES['CNAME']
    name = Name.new
    name.dns = self[:name].dns
    str = name.read(self[:rdata]).to_human
  when TYPES['SOA']
    str = human_soa_rdata
  when TYPES['MX']
    str = human_mx_data
  when TYPES['SRV']
    str = human_srv_data
  end

  str
end

#human_rrclassString

Get human readable class

Returns:

  • (String)

Since:

  • 1.3.0


79
80
81
82
83
84
85
86
87
# File 'lib/packetgen/header/dns/rr.rb', line 79

def human_rrclass
  if self[:name].dns.is_a? MDNS
    str = self.class::CLASSES.key(self.rrclass & 0x7fff) || '0x%04x' % (self.rrclass & 0x7fff)
    str += ' CACHE-FLUSH' if (self.rrclass & 0x8000).positive?
    str
  else
    self.class::CLASSES.key(self.rrclass) || '0x%04x' % self.rrclass
  end
end

#to_humanString

Returns:

  • (String)

Since:

  • 1.3.0


90
91
92
# File 'lib/packetgen/header/dns/rr.rb', line 90

def to_human
  "#{human_type} #{human_rrclass} #{name} TTL #{ttl} #{human_rdata}"
end