Class: ReDNS::Resource

Inherits:
Fragment show all
Defined in:
lib/redns/resource.rb

Instance Attribute Summary

Attributes inherited from Fragment

#attributes

Instance Method Summary collapse

Methods inherited from Fragment

attribute, #initialize

Methods included from Support

#addr_to_arpa, #bind_all_addr, #default_nameservers, #default_resolver_address, #dns_port, #inet_aton, #inet_ntoa, #io_nonblock, #io_nonblock?, #io_set_nonblock, #is_ip?

Constructor Details

This class inherits a constructor from ReDNS::Fragment

Instance Method Details

#deserialize(buffer) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/redns/resource.rb', line 54

def deserialize(buffer)
self.name = ReDNS::Name.new(buffer)

raw = buffer.unpack("nnNn")

self.rtype = ReDNS::RR_TYPE_LABEL[raw.shift]
self.rclass = ReDNS::RR_CLASS_LABEL[raw.shift]
self.ttl = raw.shift

rdata_length = raw.shift

  self.rdata =
		case (self.rtype)
		when :a, :aaaa
			self.rdata = ReDNS::Address.new(buffer)
		when :cname, :ptr, :ns
			self.rdata = ReDNS::Name.new(buffer)
		when :mx
			self.rdata = ReDNS::Record::MX.new(buffer)
		when :soa
			self.rdata = ReDNS::Record::SOA.new(buffer)
		when :null
		  self.rdata = (rdata_length and ReDNS::Record::Null.new(buffer.slice(rdata_length)))
  when :spf
    self.rdata = (rdata_length and ReDNS::Record::SPF.new(buffer.slice(rdata_length)))
		when :txt
		  self.rdata = (rdata_length and ReDNS::Record::TXT.new(buffer.slice(rdata_length)))
		else
      # FUTURE: Throw exception here when trying to decode invalid type
		  nil
		end
		
	self
end

#empty?Boolean

Instance Methods =====================================================

Returns:

  • (Boolean)


17
18
19
# File 'lib/redns/resource.rb', line 17

def empty?
  self.name.empty?
end

#serialize(buffer = ReDNS::Buffer.new) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redns/resource.rb', line 29

def serialize(buffer = ReDNS::Buffer.new)
  self.name.serialize(buffer)
  
  data_buffer = nil
  
  if (self.rdata)
 	  data_buffer = ReDNS::Buffer.new
 	  self.rdata.serialize(data_buffer)
  end
  
  buffer.pack(
     'nnNn',
		ReDNS::RR_TYPE[self.rtype],
		ReDNS::RR_CLASS[self.rclass],
		self.ttl,
		data_buffer ? data_buffer.length : 0
  )
  
  if (data_buffer)
    buffer.append(data_buffer)
   end
   
   buffer
end

#to_aObject



25
26
27
# File 'lib/redns/resource.rb', line 25

def to_a
	[ name, ttl, rclass, rtype, rdata.to_a ].flatten
end

#to_sObject



21
22
23
# File 'lib/redns/resource.rb', line 21

def to_s
	"#{name} #{ttl} #{rclass.to_s.upcase} #{rtype.to_s.upcase} #{rdata}"
end