Class: Dnsruby::RRSet

Inherits:
Object
  • Object
show all
Defined in:
lib/Dnsruby/resource/resource.rb

Overview

RFC2181, section 5 “It is however possible for most record types to exist with the same label, class and type, but with different data. Such a group of records is hereby defined to be a Resource Record Set (RRSet).” This class also stores the RRSIG records which cover the RRSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rrs = []) ⇒ RRSet

Returns a new instance of RRSet.



28
29
30
31
32
33
34
35
# File 'lib/Dnsruby/resource/resource.rb', line 28

def initialize(rrs = [])
  if (!rrs.instance_of?Array)
    rrs = [rrs]
  end
  @rrs = []
  @num_sigs = 0
  rrs.each {|rr| add(rr)}
end

Instance Attribute Details

#num_sigsObject (readonly)

The number of RRSIGs stored in this RRSet



27
28
29
# File 'lib/Dnsruby/resource/resource.rb', line 27

def num_sigs
  @num_sigs
end

Instance Method Details

#==(other) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/Dnsruby/resource/resource.rb', line 114

def ==(other)
  return false unless other.instance_of?RRSet
  return false if (other.sigs.length != self.sigs.length)
  return false if (other.rrs.length != self.rrs.length)
  return false if (other.ttl != self.ttl)
  otherrrs = other.rrs
  self.rrs.each {|rr|
    return false if (!otherrrs.include?rr)
  }
  othersigs= other.sigs
  self.sigs.each {|sig|
    return false if (!othersigs.include?sig)
  }
  return true
end

#[](index) ⇒ Object



138
139
140
# File 'lib/Dnsruby/resource/resource.rb', line 138

def [](index)
  return @rrs[index]
end

#add(r) ⇒ Object

Add the RR to this RRSet



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
88
89
90
91
92
93
94
# File 'lib/Dnsruby/resource/resource.rb', line 63

def add(r)
  if (r.instance_of?RRSet)
    ret = false
    [r.rrs, r.sigs].each {|rr| ret = add(rr)}
    return ret
  end
  r = RR.create(r.to_s) # clone the record
  if (@rrs.size() == 0) #  && !(r.type == Types.RRSIG))
    return privateAdd(r)
  end
  # Check the type, klass and ttl are correct
  first = @rrs[0]
  if (!r.sameRRset(first))
    return false
#        raise ArgumentError.new("record does not match rrset")
  end
  
  if (!(r.type == Types.RRSIG) && (!(first.type == Types.RRSIG)))
    if (r.ttl != first.ttl) # RFC2181, section 5.2
      if (r.ttl > first.ttl)
        r.ttl=(first.ttl)
      else
        @rrs.each do |rr|
          rr.ttl = r.ttl
        end
      end
    end
  end
  
    return privateAdd(r)
  return true
end

#delete(rr) ⇒ Object

Delete the RR from this RRSet



130
131
132
# File 'lib/Dnsruby/resource/resource.rb', line 130

def delete(rr)
  @rrs.delete(rr)
end

#eachObject



133
134
135
136
137
# File 'lib/Dnsruby/resource/resource.rb', line 133

def each
  @rrs.each do |rr|
    yield rr
  end
end

#klassObject

Return the klass of this RRSet



149
150
151
# File 'lib/Dnsruby/resource/resource.rb', line 149

def klass
  return @rrs[0].klass
end

#lengthObject



173
174
175
# File 'lib/Dnsruby/resource/resource.rb', line 173

def length
  return @rrs.length
end

#nameObject



163
164
165
# File 'lib/Dnsruby/resource/resource.rb', line 163

def name
  return @rrs[0].name
end

#privateAdd(r) ⇒ Object

:nodoc:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/Dnsruby/resource/resource.rb', line 44

def privateAdd(r) #:nodoc:
  if @rrs.include?r
    return true
  end      
  new_pos = @rrs.length - @num_sigs
  if ((@num_sigs == @rrs.length)  && @num_sigs > 0) # if we added RRSIG first
    if (r.type != @rrs.last.type_covered)
      return false
    end
  end
  if (r.type == Types.RRSIG)
    new_pos = @rrs.length
    @num_sigs += 1
  end
  @rrs.insert(new_pos, r)
  return true
end

#rrsObject

The RRs (not RRSIGs) stored in this RRSet



41
42
43
# File 'lib/Dnsruby/resource/resource.rb', line 41

def rrs
  return @rrs[0, @rrs.length-@num_sigs]
end

#sigsObject

The RRSIGs stored with this RRSet



37
38
39
# File 'lib/Dnsruby/resource/resource.rb', line 37

def sigs
  return @rrs[@rrs.length-@num_sigs, @num_sigs]
end

#sort_canonicalObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/Dnsruby/resource/resource.rb', line 96

def sort_canonical
  #Make a list, for all the RRs, where each RR contributes
  #the canonical RDATA encoding
  canonical_rrs = {}
  self.rrs.each do |rr|
    data = MessageEncoder.new {|msg|
      rr.encode_rdata(msg, true)
    }.to_s
    canonical_rrs[data] = rr
  end

  return_rrs = RRSet.new
  canonical_rrs.keys.sort.each { |rdata|
    return_rrs.add(canonical_rrs[rdata])
  }
  return return_rrs
end

#to_sObject



166
167
168
169
170
171
172
# File 'lib/Dnsruby/resource/resource.rb', line 166

def to_s
  ret = ""
  each {|rec|
    ret += rec.to_s + "\n"
  }
  return ret
end

#ttlObject

Return the ttl of this RRSet



153
154
155
# File 'lib/Dnsruby/resource/resource.rb', line 153

def ttl
  return @rrs[0].ttl
end

#ttl=(ttl) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/Dnsruby/resource/resource.rb', line 156

def ttl=(ttl)
  [rrs, sigs].each {|rrs|
    rrs.each {|rr|
      rr.ttl = ttl
    }
  }
end

#typeObject

Return the type of this RRSet



142
143
144
145
146
147
# File 'lib/Dnsruby/resource/resource.rb', line 142

def type
  if (@rrs[0])
  return @rrs[0].type
  end
  return nil
end