class DNSMessage::RR

Parse and build Resource Records

Constants

BUILDERS
PARSERS

Attributes

add_to_hash[R]
klass[RW]
name[RW]
opt_edns0_version[RW]
opt_rcode[RW]
opt_udp[RW]
opt_z_dnssec[RW]
rdata[RW]
size[R]
ttl[RW]
type[RW]

Public Class Methods

default_opt(size) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 60
def self.default_opt(size)
  new.tap do |opt|
    opt.name = ""
    opt.type = Type::OPT
    opt.opt_udp = size
    opt.opt_rcode = 0
    opt.opt_edns0_version = 0
    opt.opt_z_dnssec = 0
  end
end
new(name: nil, type: nil, klass: Class::IN, ttl: 0, rdata: nil) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 26
def initialize(name: nil, type: nil, klass: Class::IN, ttl: 0,
               rdata: nil)
  @name  = name
  @type  = type
  @klass = klass
  @ttl   = ttl
  @rdata = rdata
  @add_to_hash = []
end
parse(record, ptr) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 36
def self.parse(record, ptr)
  new.tap do |rr|
    rr.parse(record, ptr)
  end
end

Public Instance Methods

build(ptr, idx) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 50
def build(ptr, idx)
  return "" unless BUILDERS[type]

  name_bytes, add = Name.build(@name, ptr)
  ptr.add(@name, idx) if add
  data = send(builder(@type), ptr, idx + name_bytes.length)
  @rdata_length = data.length
  name_bytes + [@type, @klass, @ttl, @rdata_length].pack("nnNn") + data
end
parse(record, ptr) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 42
def parse(record, ptr)
  @name, idx, add = Name.parse(record, ptr)
  @add_to_hash << [idx, @name] if add
  @type, @klass, @ttl, rdata_length = record[idx...idx + 10].unpack("nnNn")
  @rdata = send(parser(@type), record, idx + 10, rdata_length, ptr)
  @size = idx + 10 + rdata_length
end

Private Instance Methods

build_ip(_ptr, _) click to toggle source

Builders

# File lib/dnsmessage/resource_record.rb, line 111
def build_ip(_ptr, _)
  @rdata.hton
end
build_name(ptr, idx) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 127
def build_name(ptr, idx)
  Name.build(@rdata, ptr).tap do |bytes, add|
    ptr.add(@rdata, idx) if add
    return bytes
  end
end
build_opt(_ptr, _) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 115
def build_opt(_ptr, _)
  @klass = @opt_udp
  @ttl = [@opt_rcode,
          @opt_edns0_version,
          @opt_z_dnssec << 15].pack("CCn").unpack1("N")
  "" # Set RDATA to nothing
end
build_text(_ptr, _) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 123
def build_text(_ptr, _)
  @rdata.length.chr + rdata
end
builder(type) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 77
def builder(type)
  BUILDERS[type]
end
parse_ip(rdata, start, length, _ptr) click to toggle source

Parsers

# File lib/dnsmessage/resource_record.rb, line 85
def parse_ip(rdata, start, length, _ptr)
  IPAddr.new_ntoh(rdata[start...start + length])
end
parse_name(rdata, start, length, ptr) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 101
def parse_name(rdata, start, length, ptr)
  name, idx, add = Name.parse(rdata[0...length], ptr)
  @add_to_hash << [start + idx, name] if add
  name
end
parse_opt(_rdata, _start, _length, _ptr) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 89
def parse_opt(_rdata, _start, _length, _ptr)
  @opt_udp = @klass
  @opt_rcode, @opt_edns0_version, @opt_z_dnssec =
    [@ttl].pack("N").unpack("CCn")
  @opt_z_dnssec = @opt_z_dnssec >> 15
end
parse_text(rdata, start, _length, _ptr) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 96
def parse_text(rdata, start, _length, _ptr)
  txt_length = rdata[start].ord
  rdata[start + 1..start + txt_length]
end
parser(type) click to toggle source
# File lib/dnsmessage/resource_record.rb, line 73
def parser(type)
  PARSERS[type]
end