Parent

Class Index [+]

Quicksearch

Dnsruby::RR

Superclass for all Dnsruby resource records.

Represents a DNS RR (resource record) [RFC1035, section 3.2]

Use Dnsruby::RR::create(...) to create a new RR record.

   mx = Dnsruby::RR.create("example.com. 7200 MX 10 mailhost.example.com.")

   rr = Dnsruby::RR.create({:name => "example.com", :type => "MX", :ttl => 7200,
                                  :preference => 10, :exchange => "mailhost.example.com"})

   s = rr.to_s # Get a String representation of the RR (in zone file format)
   rr_again = Dnsruby::RR.create(s)

Attributes

name[R]

The Resource’s domain name

type[R]

The Resource type

klass[R]

The Resource class

ttl[RW]

The Resource Time-To-Live

rdata[RW]

The Resource data section

Public Class Methods

create(*args) click to toggle source

Create a new RR from the arguments, which can be either a String or a Hash. See new_from_string and new_from_hash for details

   a     = Dnsruby::RR.create("foo.example.com. 86400 A 10.1.2.3")
   mx    = Dnsruby::RR.create("example.com. 7200 MX 10 mailhost.example.com.")
   cname = Dnsruby::RR.create("www.example.com 300 IN CNAME www1.example.com")
   txt   = Dnsruby::RR.create('baz.example.com 3600 HS TXT "text record"')

   rr = Dnsruby::RR.create({:name => "example.com"})
   rr = Dnsruby::RR.create({:name => "example.com", :type => "MX", :ttl => 10,
                                  :preference => 5, :exchange => "mx1.example.com"})
     # File lib/Dnsruby/resource/resource.rb, line 630
630:     def RR.create(*args)
631:       if (args.length == 1) && (args[0].class == String)
632:         return new_from_string(args[0])
633:       elsif (args.length == 1) && (args[0].class == Hash)
634:         return new_from_hash(args[0])
635:       else
636:         return new_from_data(args)
637:       end
638:     end
get_num(bytes) click to toggle source
     # File lib/Dnsruby/resource/resource.rb, line 640
640:     def self.get_num(bytes)
641:       ret = 0
642:       shift = (bytes.length-1) * 8
643:       bytes.each_byte {|byte|
644:         ret += byte.to_i << shift
645:         shift -= 8
646:       }
647:       return ret
648:     end
implemented_rrs() click to toggle source

Return an array of all the currently implemented RR types

     # File lib/Dnsruby/resource/resource.rb, line 485
485:     def RR.implemented_rrs
486:       if (!@@implemented_rr_map)
487:         @@implemented_rr_map = ClassHash.keys.map {|k| Dnsruby::Types.to_string(k[0])}
488:       end
489:       return @@implemented_rr_map
490:     end
new_from_hash(inhash) click to toggle source

Create a new RR from the hash. The name is required; all other fields are optional. Type defaults to ANY and the Class defaults to IN. The TTL defaults to 0.

If the type is specified, then it is necessary to provide ALL of the resource record fields which are specific to that record; i.e. for an MX record, you would need to specify the exchange and the preference

   require 'Dnsruby'
   rr = Dnsruby::RR.new_from_hash({:name => "example.com"})
   rr = Dnsruby::RR.new_from_hash({:name => "example.com", :type => Types.MX, :ttl => 10, :preference => 5, :exchange => "mx1.example.com"})
     # File lib/Dnsruby/resource/resource.rb, line 330
330:     def RR.new_from_hash(inhash)
331:       hash = inhash.clone
332:       type = hash[:type] || Types::ANY
333:       klass = hash[:klass] || Classes::IN
334:       ttl = hash[:ttl] || 0
335:       recordclass = get_class(type, klass)
336:       record = recordclass.new
337:       record.name=hash[:name]
338:       if !(record.name.kind_of?Name)
339:         record.name = Name.create(record.name)
340:       end
341:       record.ttl=ttl
342:       record.type = type
343:       record.klass = klass
344:       hash.delete(:name)
345:       hash.delete(:type)
346:       hash.delete(:ttl)
347:       hash.delete(:klass)
348:       record.from_hash(hash)
349:       return record
350:     end
new_from_string(rrstring) click to toggle source

Returns a Dnsruby::RR object of the appropriate type and initialized from the string passed by the user. The format of the string is that used in zone files, and is compatible with the string returned by Net::DNS::RR.inspect

The name and RR type are required; all other information is optional. If omitted, the TTL defaults to 0 and the RR class defaults to IN.

All names must be fully qualified. The trailing dot (.) is optional.

   a     = Dnsruby::RR.new_from_string("foo.example.com. 86400 A 10.1.2.3")
   mx    = Dnsruby::RR.new_from_string("example.com. 7200 MX 10 mailhost.example.com.")
   cname = Dnsruby::RR.new_from_string("www.example.com 300 IN CNAME www1.example.com")
   txt   = Dnsruby::RR.new_from_string('baz.example.com 3600 HS TXT "text record"')
     # File lib/Dnsruby/resource/resource.rb, line 369
369:     def RR.new_from_string(rrstring)
370:       # strip out comments
371:       # Test for non escaped ";" by means of the look-behind assertion
372:       # (the backslash is escaped)
373:       rrstring.gsub!(/(\?<!\\);.*/, "");
374:       
375:       if ((rrstring =~/#{@@RR_REGEX}/o) == nil)
376:         raise Exception, "#{rrstring} did not match RR pat.\nPlease report this to the author!\n"
377:       end
378:       
379:       name    = $1;
380:       ttl     = $2.to_i || 0;
381:       rrclass = $3 || '';
382:       
383:       
384:       rrtype  = $4 || '';
385:       rdata   = $5 || '';
386:       
387:       if rdata
388:         rdata.gsub!(/\s+$/, "")
389:       end
390:       
391:       # RFC3597 tweaks
392:       # This converts to known class and type if specified as TYPE###
393:       if rrtype  =~/^TYPE\d+/
394:         rrtype  = Dnsruby::Types.typesbyval(Dnsruby::Types::typesbyname(rrtype))
395:       end
396:       if rrclass =~/^CLASS\d+/
397:         rrclass = Dnsruby::Classes.classesbyval(Dnsruby::Classes::classesbyname(rrclass))
398:       end
399:       
400:       
401:       if (rrtype=='' && rrclass && rrclass == 'ANY')
402:         rrtype  = 'ANY';
403:         rrclass = 'IN';
404:       elsif (rrclass=='')
405:         rrclass = 'IN';
406:       end
407:       
408:       if (rrtype == '')
409:         rrtype = 'ANY';
410:       end
411: 
412:       if ((rrtype == "NAPTR") || (rrtype == "TXT"))
413:       else
414:         if (rdata)
415:         rdata.gsub!("(", "")
416:         rdata.gsub!(")", "")
417:         end
418:       end
419:       
420:       if (implemented_rrs.include?(rrtype) && rdata !~/^\s*\\#/ )
421:         subclass = _get_subclass(name, rrtype, rrclass, ttl, rdata)
422:         return subclass
423:       elsif (implemented_rrs.include?(rrtype))   # A known RR type starting with \#
424:         rdata =~ /\\\#\s+(\d+)\s+(.*)$/;
425:         
426:         rdlength = $1.to_i;
427:         hexdump  = $2;
428:         hexdump.gsub!(/\s*/, "");
429:         
430:         if hexdump.length() != rdlength*2
431:           raise Exception, "#{rdata} is inconsistent; length does not match content"
432:         end
433:         
434:         rdata = [hexdump].pack('H*');
435:         
436:         return new_from_data(name, rrtype, rrclass, ttl, rdlength, rdata, 0) # rdata.length() - rdlength);
437:       elsif (rdata=~/\s*\\\#\s+\d+\s+/)
438:         #We are now dealing with the truly unknown.
439:         raise Exception, 'Expected RFC3597 representation of RDATA' unless rdata =~/\\\#\s+(\d+)\s+(.*)$/;
440:         
441:         rdlength = $1.to_i;
442:         hexdump  = $2;
443:         hexdump.gsub!(/\s*/, "");
444:         
445:         if hexdump.length() != rdlength*2
446:           raise Exception, "#{rdata} is inconsistent; length does not match content" ;
447:         end
448:         
449:         rdata = [hexdump].pack('H*');
450:         
451:         return new_from_data(name,rrtype,rrclass,ttl,rdlength,rdata,0) # rdata.length() - rdlength);
452:       else
453:         #God knows how to handle these...
454:         subclass = _get_subclass(name, rrtype, rrclass, ttl, "")
455:         return subclass
456:       end
457:     end

Public Instance Methods

==(other) click to toggle source
     # File lib/Dnsruby/resource/resource.rb, line 538
538:     def ==(other)
539:       return false unless self.class == other.class
540:       ivars = self.instance_variables
541:       s_ivars = []
542:       ivars.each {|i| s_ivars << i.to_s} # Ruby 1.9
543:       s_ivars.delete "@ttl" # RFC 2136 section 1.1
544:       s_ivars.delete "@rdata"
545:       if (self.type == Types.DS)
546:         s_ivars.delete "@digest"
547:       end
548:       s_ivars.sort!
549:       
550:       ivars = other.instance_variables
551:       o_ivars = []
552:       ivars.each {|i| o_ivars << i.to_s} # Ruby 1.9
553:       o_ivars.delete "@ttl" # RFC 2136 section 1.1
554:       o_ivars.delete "@rdata"
555:       if (other.type == Types.DS)
556:         o_ivars.delete "@digest"
557:       end
558:       o_ivars.sort!
559:       
560:       return s_ivars == o_ivars &&
561:         s_ivars.collect {|name| self.instance_variable_get name} ==
562:         o_ivars.collect {|name| other.instance_variable_get name}
563:     end
clone() click to toggle source
     # File lib/Dnsruby/resource/resource.rb, line 263
263:     def clone
264:       MessageDecoder.new(MessageEncoder.new {|msg|
265:           msg.put_rr(self, true)}.to_s) {|msg|
266:         r = msg.get_rr
267:         return r
268:       }
269: 
270:     end
init_defaults() click to toggle source
     # File lib/Dnsruby/resource/resource.rb, line 287
287:     def init_defaults
288:       # Default to do nothing
289:     end
klass=(klass) click to toggle source
     # File lib/Dnsruby/resource/resource.rb, line 251
251:     def klass=(klass)
252:       if (@type != Types::OPT)
253:         @klass= Classes.new(klass)
254:       else
255:         if (klass.class == Classes)
256:           @klass = klass
257:         else
258:           @klass = Classes.new("CLASS#{klass}")
259:         end
260:       end
261:     end
name=(newname) click to toggle source
     # File lib/Dnsruby/resource/resource.rb, line 238
238:     def name=(newname)
239:       if (!(newname.kind_of?Name))
240:         @name=Name.create(newname)
241:       else
242:         @name = newname
243:       end
244:     end
rdata_to_string() click to toggle source

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

     # File lib/Dnsruby/resource/resource.rb, line 510
510:     def rdata_to_string
511:       if (@rdata && @rdata.length > 0)
512:         return @rdata
513:       else
514:         return "no rdata"
515:       end
516:     end
rdlength() click to toggle source
     # File lib/Dnsruby/resource/resource.rb, line 234
234:     def rdlength
235:       return rdata.length
236:     end
sameRRset(rec) click to toggle source

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.

     # File lib/Dnsruby/resource/resource.rb, line 275
275:     def sameRRset(rec)
276:       if (@klass != rec.klass || @name.downcase != rec.name.downcase)
277:         return false
278:       end
279:       [rec, self].each { |rr|
280:         if (rr.type == Types::RRSIG)
281:           return ((@type == rr.type_covered) || (rec.type == rr.type_covered))
282:         end
283:       }
284:       return (@type == rec.type)
285:     end
to_s() click to toggle source

Returns a string representation of the RR in zone file format

     # File lib/Dnsruby/resource/resource.rb, line 505
505:     def to_s
506:       return (@name ? @name.to_s(true):"") + "\t" +(@ttl ? @ttl.to_s():"") + "\t" + (klass() ? klass.to_s():"") + "\t" + (type() ? type.to_s():"") + "\t" + rdata_to_string
507:     end
type=(type) click to toggle source
     # File lib/Dnsruby/resource/resource.rb, line 246
246:     def type=(type)
247:       @type = Types.new(type)
248:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.