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

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

Overview

DNS Ressource Record

Author:

  • Sylvain Daubert

  • LemonTree55

Since:

  • 1.3.0

Direct Known Subclasses

OPT

Defined Under Namespace

Classes: MX, RData, SOA, SRV

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?

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



73
74
75
76
77
78
# File 'lib/packetgen/header/dns/rr.rb', line 73

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

  self.rdata = options[:rdata]
end

Instance Attribute Details

#rdataBinStruct::String

Returns:

  • (BinStruct::String)


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

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

#rdlengthInteger

16-bit #rdata length

Returns:

  • (Integer)


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

define_attr :rdlength, BinStruct::Int16

#ttlInteger

32-bit time to live

Returns:

  • (Integer)


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

define_attr :ttl, BinStruct::Int32

Instance Method Details

#human_rdataString

Get human readable rdata

Returns:

  • (String)

Since:

  • 1.3.0



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/packetgen/header/dns/rr.rb', line 94

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

  case type
  when TYPES['NS'], TYPES['PTR'], TYPES['CNAME']
    name = Name.new(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



115
116
117
118
119
120
121
122
123
# File 'lib/packetgen/header/dns/rr.rb', line 115

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.anybits?(0x8000)
    str
  else
    self.class::CLASSES.key(self.rrclass) || '0x%04x' % self.rrclass
  end
end

#to_humanString

Returns:

  • (String)

Since:

  • 1.3.0



126
127
128
# File 'lib/packetgen/header/dns/rr.rb', line 126

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