Class: Dnsruby::Section

Inherits:
Array
  • Object
show all
Defined in:
lib/dnsruby/message/section.rb

Instance Method Summary collapse

Constructor Details

#initialize(msg = nil) ⇒ Section

Returns a new instance of Section.



4
5
6
7
# File 'lib/dnsruby/message/section.rb', line 4

def initialize(msg = nil)
  @msg = msg
  super(0)
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dnsruby/message/section.rb', line 45

def ==(other)
  return false unless self.class == other.class
  return false if other.rrsets(nil, true).length != self.rrsets(nil, true).length

  otherrrsets = other.rrsets(nil)
  self.rrsets(nil).each {|rrset|
    return false unless otherrrsets.include?(rrset)
  }

  true
end

#remove_rrset(name, type) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dnsruby/message/section.rb', line 57

def remove_rrset(name, type)
  #  Remove all RRs with the name and type from the section.
  #  Need to worry about header counts here - can we get Message to
  #  update the counts itself, rather than the section worrying about it?
  rrs_to_delete = []
  each do |rr|
    next if rr.rr_type == Types::OPT
    if (rr.name.to_s.downcase == name.to_s.downcase) &&
        ((rr.type == type) ||
            ((rr.type == Types::RRSIG) && (rr.type_covered == type)))
      rrs_to_delete.push(rr)
    end
  end
  rrs_to_delete.each { |rr| delete(rr) }
  @msg.update_counts if @msg
end

#rrset(name, type = Types.A, klass = Classes::IN) ⇒ Object

Return the rrset of the specified type in this section



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dnsruby/message/section.rb', line 10

def rrset(name, type=Types.A, klass=Classes::IN)
  rrs = select{|rr|
    type_ok = (rr.type==type)
    if rr.type == Types::RRSIG
      type_ok = (rr.type_covered == type)
    end
    unless /\.\z/ =~ name.to_s
      name = name.to_s + '.'
    end
    type_ok && (rr.klass == klass) && (rr.name.to_s(true).downcase == name.to_s().downcase)
  }
  RRSet.new(rrs)
end

#rrsets(type = nil, include_opt = false) ⇒ Object

Return an array of all the rrsets in the section



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dnsruby/message/section.rb', line 25

def rrsets(type = nil, include_opt = false)
  if type && !(Types === type)
    type = Types.new(type)
  end
  groups = {}
  budget = ValidationBudget.current if defined?(ValidationBudget)
  each do |rr|
    budget&.check!(1)
    next if !include_opt && rr.type == Types::OPT
    covered_type = rr.type == Types::RRSIG ? rr.type_covered : rr.type
    next if type && rr.type != type && covered_type != type

    # Hash insertion order preserves the section's first occurrence of each
    # set. A signature joins the set it covers even when it appears first.
    key = [rr.name.canonical, rr.klass.code, covered_type.code]
    (groups[key] ||= []) << rr
  end
  groups.values.map { |records| RRSet.new(records) }
end