Class: Dnsruby::RRSet

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/dnsruby/resource/RRSet.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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rrs = []) ⇒ RRSet

Returns a new instance of RRSet.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dnsruby/resource/RRSet.rb', line 13

def initialize(rrs = [])
  if (!rrs.instance_of?Array)
    rrs = [rrs]
  end
  @rrs = []
  @num_sigs = 0
  # During construction all entries are private wire copies. Discard this
  # temporary index before exposing the set: public RR objects are mutable.
  @membership_index = {}
  rrs.each {|rr| add(rr)}
ensure
  @membership_index = nil
end

Instance Attribute Details

#num_sigs ⇒ Object (readonly)

The number of RRSIGs stored in this RRSet



12
13
14
# File 'lib/dnsruby/resource/RRSet.rb', line 12

def num_sigs
  @num_sigs
end

Class Method Details

.new_from_string(string) ⇒ Object



26
27
28
29
30
31
# File 'lib/dnsruby/resource/RRSet.rb', line 26

def self.new_from_string(string)
  rr_strings = string.split("\n")
  rrs = rr_strings.map { |s| Dnsruby::RR.new_from_string(s) }

  Dnsruby::RRSet.new(rrs)
end

Instance Method Details

#<=>(other) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/dnsruby/resource/RRSet.rb', line 102

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

#==(other) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dnsruby/resource/RRSet.rb', line 137

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



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

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

#add(rin, do_clone = true) ⇒ Object

Add the RR to this RRSet Takes a copy of the RR by default. To suppress this, pass false as the second parameter.



69
70
71
72
73
74
75
76
77
# File 'lib/dnsruby/resource/RRSet.rb', line 69

def add(rin, do_clone = true)
  if (rin.instance_of?RRSet)
    ret = false
    (rin.rrs + rin.sigs).each {|rr| ret = add(rr, do_clone)}
    return ret
  end
  @membership_index = nil unless do_clone
  add_record(rin, do_clone)
end

#delete(rr) ⇒ Object

Delete the RR from this RRSet



153
154
155
156
157
# File 'lib/dnsruby/resource/RRSet.rb', line 153

def delete(rr)
  removed = @rrs.delete(rr)
  @num_sigs = @rrs.count { |record| record.type == Types::RRSIG } if removed
  removed
end

#each ⇒ Object



158
159
160
161
162
# File 'lib/dnsruby/resource/RRSet.rb', line 158

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

#klass ⇒ Object

Return the klass of this RRSet



174
175
176
# File 'lib/dnsruby/resource/RRSet.rb', line 174

def klass
  return @rrs[0].klass
end

#length ⇒ Object



202
203
204
# File 'lib/dnsruby/resource/RRSet.rb', line 202

def length
  return @rrs.length
end

#name ⇒ Object



188
189
190
191
192
193
194
# File 'lib/dnsruby/resource/RRSet.rb', line 188

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

#privateAdd(r) ⇒ Object

:nodoc:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dnsruby/resource/RRSet.rb', line 39

def privateAdd(r) #:nodoc:
  budget = ValidationBudget.current if defined?(ValidationBudget)
  duplicate = if @membership_index
    budget&.check!(1)
    @membership_index.key?(r)
  else
    @rrs.any? { |existing| budget&.check!(1); existing == r }
  end
  if duplicate
    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) && (r.type != Types.RRSIG))||
        ((r.type == Types.RRSIG) && (r.type_covered != @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)
  @membership_index[r] = true if @membership_index
  return true
end

#rrs ⇒ Object

The RRs (not RRSIGs) stored in this RRSet



36
37
38
# File 'lib/dnsruby/resource/RRSet.rb', line 36

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

#sigs ⇒ Object

The RRSIGs stored with this RRSet



32
33
34
# File 'lib/dnsruby/resource/RRSet.rb', line 32

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

#sort_canonical ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dnsruby/resource/RRSet.rb', line 112

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
  return_rrs.send(:add_canonical_records, canonical_rrs.keys.sort.map { |rdata| canonical_rrs[rdata] })
  return return_rrs
end

#to_s ⇒ Object



195
196
197
198
199
200
201
# File 'lib/dnsruby/resource/RRSet.rb', line 195

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

#ttl ⇒ Object

Return the ttl of this RRSet



178
179
180
# File 'lib/dnsruby/resource/RRSet.rb', line 178

def ttl
  return @rrs[0].ttl
end

#ttl=(ttl) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/dnsruby/resource/RRSet.rb', line 181

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

#type ⇒ Object

Return the type of this RRSet



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

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