Class: Dnsruby::RR

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/dnsruby/resource/A.rb,
lib/dnsruby/resource/DS.rb,
lib/dnsruby/resource/IN.rb,
lib/dnsruby/resource/KX.rb,
lib/dnsruby/resource/MX.rb,
lib/dnsruby/resource/PX.rb,
lib/dnsruby/resource/RP.rb,
lib/dnsruby/resource/RR.rb,
lib/dnsruby/resource/RT.rb,
lib/dnsruby/resource/APL.rb,
lib/dnsruby/resource/CAA.rb,
lib/dnsruby/resource/CDS.rb,
lib/dnsruby/resource/DLV.rb,
lib/dnsruby/resource/HIP.rb,
lib/dnsruby/resource/LOC.rb,
lib/dnsruby/resource/NXT.rb,
lib/dnsruby/resource/OPT.rb,
lib/dnsruby/resource/SOA.rb,
lib/dnsruby/resource/SPF.rb,
lib/dnsruby/resource/SRV.rb,
lib/dnsruby/resource/TXT.rb,
lib/dnsruby/resource/URI.rb,
lib/dnsruby/resource/X25.rb,
lib/dnsruby/resource/AAAA.rb,
lib/dnsruby/resource/CERT.rb,
lib/dnsruby/resource/GPOS.rb,
lib/dnsruby/resource/ISDN.rb,
lib/dnsruby/resource/NSAP.rb,
lib/dnsruby/resource/NSEC.rb,
lib/dnsruby/resource/TKEY.rb,
lib/dnsruby/resource/TLSA.rb,
lib/dnsruby/resource/TSIG.rb,
lib/dnsruby/resource/AFSDB.rb,
lib/dnsruby/resource/DHCID.rb,
lib/dnsruby/resource/HINFO.rb,
lib/dnsruby/resource/MINFO.rb,
lib/dnsruby/resource/NAPTR.rb,
lib/dnsruby/resource/NSEC3.rb,
lib/dnsruby/resource/RRSIG.rb,
lib/dnsruby/resource/SSHFP.rb,
lib/dnsruby/resource/DNSKEY.rb,
lib/dnsruby/resource/CDNSKEY.rb,
lib/dnsruby/resource/generic.rb,
lib/dnsruby/resource/IPSECKEY.rb,
lib/dnsruby/resource/NSEC3PARAM.rb,
lib/dnsruby/resource/domain_name.rb

Direct Known Subclasses

IN::A, IN::AAAA, IN::AFSDB, IN::APL, IN::PX, IN::SRV, IN::TLSA, IN::WKS

Defined Under Namespace

Modules: IN Classes: ANY, CAA, CDNSKEY, CDS, CERT, CNAME, DHCID, DLV, DNAME, DNSKEY, DS, DomainName, GPOS, Generic, HINFO, HIP, IPSECKEY, ISDN, KX, LOC, MB, MG, MINFO, MR, MX, NAPTR, NS, NSAP, NSEC, NSEC3, NSEC3PARAM, NXT, OPT, PTR, RP, RRSIG, RT, SOA, SPF, SSHFP, TKEY, TSIG, TXT, URI, X25

Constant Summary collapse

ClassInsensitiveTypes =
{
  Types::NS => NS,
  Types::CNAME => CNAME,
  Types::DNAME => DNAME,
  Types::URI => URI,
  Types::DS => DS,
  Types::CDS => CDS,
  Types::DNSKEY => DNSKEY,
  Types::CDNSKEY => CDNSKEY,
  Types::SOA => SOA,
  Types::PTR => PTR,
  Types::HINFO => HINFO,
  Types::MINFO => MINFO,
  Types::MX => MX,
  Types::TXT => TXT,
  Types::ISDN => ISDN,
  Types::MB => MB,
  Types::MG => MG,
  Types::MR => MR,
  Types::NAPTR => NAPTR,
  Types::NSAP => NSAP,
  Types::OPT => OPT,
  Types::RP => RP,
  Types::RT => RT,
  Types::X25 => X25,
  Types::KX => KX,
  Types::SPF => SPF,
  Types::CERT => CERT,
  Types::LOC => LOC,
  Types::TSIG => TSIG,
  Types::TKEY => TKEY,
  Types::ANY => ANY,
  Types::RRSIG => RRSIG,
  Types::NSEC => NSEC,
  Types::NSEC3 => NSEC3,
  Types::NSEC3PARAM => NSEC3PARAM,
  Types::DLV => DLV,
  Types::SSHFP => SSHFP,
  Types::IPSECKEY => IPSECKEY,
  Types::HIP => HIP,
  Types::DHCID => DHCID,
  Types::GPOS => GPOS,
  Types::NXT => NXT,
  Types::CAA => CAA,
}
@@RR_REGEX =

A regular expression which catches any valid resource record.

Regexp.new("^\\s*(\\S+)\\s*(\\d+)?\\s*(#{Classes.regexp +
"|CLASS\\d+"})?\\s*(#{Types.regexp + '|TYPE\\d+'})?\\s*([\\s\\S]*)\$")
@@implemented_rr_map =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#klassObject

The Resource class



46
47
48
# File 'lib/dnsruby/resource/RR.rb', line 46

def klass
  @klass
end

#nameObject

The Resource's domain name



40
41
42
# File 'lib/dnsruby/resource/RR.rb', line 40

def name
  @name
end

#rdataObject

The Resource data section



52
53
54
# File 'lib/dnsruby/resource/RR.rb', line 52

def rdata
  @rdata
end

#ttlObject

The Resource Time-To-Live



49
50
51
# File 'lib/dnsruby/resource/RR.rb', line 49

def ttl
  @ttl
end

#typeObject Also known as: rr_type

The Resource type



43
44
45
# File 'lib/dnsruby/resource/RR.rb', line 43

def type
  @type
end

Class Method Details

.decode_rdata(msg) ⇒ Object

:nodoc: all

Raises:



317
318
319
320
# File 'lib/dnsruby/resource/RR.rb', line 317

def self.decode_rdata(msg) #:nodoc: all
  #  to be implemented by subclasses
  raise DecodeError.new("#{self.class} is RR.")
end

.find_class(type_value, class_value) ⇒ Object

:nodoc: all



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/dnsruby/resource/RR.rb', line 359

def self.find_class(type_value, class_value) # :nodoc: all
  if !! (ret = ClassHash[[type_value, class_value]])
    return ret
  elsif !! (val = ClassInsensitiveTypes[type_value])
    klass = Class.new(val)
    klass.const_set(:TypeValue, type_value)
    klass.const_set(:ClassValue, class_value)
    return klass
  else
    return Generic.create(type_value, class_value)
  end
end

.get_class(type_value, class_value) ⇒ Object

Get an RR of the specified type and class



373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/dnsruby/resource/RR.rb', line 373

def self.get_class(type_value, class_value) #:nodoc: all
  if type_value == Types::OPT
    return Class.new(OPT)
  elsif type_value.class == Class
    type_value = type_value.const_get(:TypeValue)
    return find_class(type_value, Classes.to_code(class_value))
  else
    type_value = (type_value.class == Types) ? type_value.code : Types.new(type_value).code
    class_value = (class_value.class == Classes) ? class_value.code : Classes.new(class_value).code
    return find_class(type_value, class_value)
  end
end

.get_num(bytes) ⇒ Object



410
411
412
413
414
415
416
417
418
# File 'lib/dnsruby/resource/RR.rb', line 410

def self.get_num(bytes)
  ret = 0
  shift = (bytes.length - 1) * 8
  bytes.each_byte do |byte|
    ret += byte.to_i << shift
    shift -= 8
  end
  ret
end

Instance Method Details

#<=>(other) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/dnsruby/resource/RR.rb', line 23

def <=>(other)
  #       return 1 if ((!other) || !(other.name) || !(other.type))
  #       return -1 if (!@name)
  if @name.canonical == other.name.canonical
    @type.code != other.type.code ? (@type.code <=> other.type.code) : (@rdata <=> other.rdata)
  else
    @name <=> other.name
  end
end

#==(other) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/dnsruby/resource/RR.rb', line 322

def ==(other)
  return false unless self.class == other.class

  ivars_to_compare = ->(object) do
    ivars = object.instance_variables.map { |var| var.to_s }
    ivars.delete '@ttl' # RFC 2136 section 1.1
    ivars.delete '@rdata'
    if self.type == Types.DS
      ivars.delete '@digest'
    end
    ivars.sort
  end

  get_instance_var_values = ->(object, ivar_names) do
    ivar_names.map { |ivar_name| object.instance_variable_get(ivar_name) }
  end

  self_ivars  = ivars_to_compare.(self)
  other_ivars = ivars_to_compare.(other)
  return false unless self_ivars == other_ivars

  self_values  = get_instance_var_values.(self, self_ivars)
  other_values = get_instance_var_values.(other, other_ivars)
  self_values == other_values
end

#cloneObject



75
76
77
78
# File 'lib/dnsruby/resource/RR.rb', line 75

def clone
  encoded = MessageEncoder.new { |encoder| encoder.put_rr(self, true) }.to_s
  MessageDecoder.new(encoded).get_rr
end

#encode_rdata(msg, canonical = false) ⇒ Object

:nodoc: all

Raises:



312
313
314
315
# File 'lib/dnsruby/resource/RR.rb', line 312

def encode_rdata(msg, canonical=false) #:nodoc: all
  #  to be implemented by subclasses
  raise EncodeError.new("#{self.class} is RR.")
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


348
349
350
# File 'lib/dnsruby/resource/RR.rb', line 348

def eql?(other) #:nodoc:
  self == other
end

#from_data(data) ⇒ Object

:nodoc: all

Raises:

  • (NotImplementedError)


302
303
304
305
# File 'lib/dnsruby/resource/RR.rb', line 302

def from_data(data) #:nodoc: all
  #  to be implemented by subclasses
  raise NotImplementedError.new
end

#from_hash(hash) ⇒ Object

:nodoc: all



125
126
127
128
129
# File 'lib/dnsruby/resource/RR.rb', line 125

def from_hash(hash) #:nodoc: all
  hash.keys.each do |param|
    send("#{param}=", hash[param])
  end
end

#from_string(input) ⇒ Object

:nodoc: all



307
308
309
310
# File 'lib/dnsruby/resource/RR.rb', line 307

def from_string(input) #:nodoc: all
  #  to be implemented by subclasses
  #       raise NotImplementedError.new
end

#hashObject

:nodoc:



352
353
354
355
356
357
# File 'lib/dnsruby/resource/RR.rb', line 352

def hash # :nodoc:
  vars = (self.instance_variables - [:@ttl, :@rdata]).sort
  vars.inject(0) do |hash_value, var_name|
    hash_value ^ self.instance_variable_get(var_name).hash
  end
end

#init_defaultsObject



98
99
100
# File 'lib/dnsruby/resource/RR.rb', line 98

def init_defaults
  #  Default to do nothing
end

#rdata_to_stringObject

Get a string representation of the data section of the RR (in zone file format)



298
299
300
# File 'lib/dnsruby/resource/RR.rb', line 298

def rdata_to_string
  (@rdata && @rdata.length > 0) ? @rdata : 'no rdata'
end

#rdlengthObject



54
55
56
# File 'lib/dnsruby/resource/RR.rb', line 54

def rdlength
  rdata.length
end

#sameRRset(rec) ⇒ Object

Determines if two Records could be part of the same RRset. This compares the name, type, and class of the Records; the ttl and rdata are not compared.



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dnsruby/resource/RR.rb', line 84

def sameRRset(rec)
  if @klass != rec.klass || @name.downcase != rec.name.downcase
    return false
  elsif (rec.type == Types.RRSIG) && (@type == Types.RRSIG)
    return rec.type_covered == self.type_covered
  end
  [rec, self].each do |rr|
    if rr.type == Types::RRSIG
      return (@type == rr.type_covered) || (rec.type == rr.type_covered)
    end
  end
  @type == rec.type
end

#to_sObject

Returns a string representation of the RR in zone file format



292
293
294
295
# File 'lib/dnsruby/resource/RR.rb', line 292

def to_s
  s = name ? (name.to_s(true) + "\t") : ''
  s << [ttl, klass, type, rdata_to_string].map(&:to_s).join("\t")
end