Class: Dnsruby::MessageEncoder

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

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ MessageEncoder

Returns a new instance of MessageEncoder.

Yields:

  • (_self)

Yield Parameters:



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

def initialize
  @data = ''.b
  @names = {}
  yield self if block_given?
end

Instance Method Details

#put_bytes(d) ⇒ Object



13
14
15
# File 'lib/dnsruby/message/encoder.rb', line 13

def put_bytes(d)
  @data << d.b
end

#put_label(d) ⇒ Object

Raises:



87
88
89
90
91
92
# File 'lib/dnsruby/message/encoder.rb', line 87

def put_label(d)
  #       s, = Name.encode(d)
  s = d
  raise EncodeError, "Label exceeds 63 octets: #{s.string.bytesize}" if s.string.bytesize > 63
  self.put_string(s.string)
end

#put_labels(d, do_canonical) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dnsruby/message/encoder.rb', line 62

def put_labels(d, do_canonical)
  if do_canonical
    d.each { |label| put_label(label) }
    @data << "\0"
    return
  end
  # Snapshot each label once. Suffix keys share these immutable strings,
  # while retaining label boundaries and case for lossless compression.
  labels = d.map { |label| label.string.b.freeze }
  d.each_index do |i|
    domain = labels[i..-1].freeze
    if (idx = @names[domain]) && idx < 0x4000
      self.put_pack('n', 0xc000 | idx)
      return
    else
      # Compression pointers have only 14 bits for the target offset.
      # Later names can still use an earlier eligible suffix.
      @names[domain] ||= @data.bytesize if @data.bytesize < 0x4000
      self.put_label(d[i])
    end
  end
  @data << "\0"
end

#put_length16 ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dnsruby/message/encoder.rb', line 25

def put_length16
  length_index = @data.length
  @data << "\0\0"
  data_start = @data.bytesize
  yield
  data_end = @data.bytesize
  length = data_end - data_start
  if length > 0xffff
    raise EncodeError.new("Length-prefixed data exceeds 65535 octets: #{length}")
  end
  @data[length_index, 2] = [length].pack("n")
end

#put_name(d, canonical = false, downcase = canonical) ⇒ Object



55
56
57
58
59
60
# File 'lib/dnsruby/message/encoder.rb', line 55

def put_name(d, canonical = false, downcase = canonical)
  #  DNSSEC requires some records (e.g. NSEC, RRSIG) to be canonicalised, but
  #  not downcased. YUK!
  d = d.downcase if downcase
  put_labels(d.to_a, canonical)
end

#put_pack(template, *d) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/dnsruby/message/encoder.rb', line 17

def put_pack(template, *d)
  begin
    @data << d.pack(template)
  rescue Encoding::CompatibilityError
    raise Dnsruby::EncodeError.new("IDN support currently requires punycode string")
  end
end

#put_rr(rr, canonical = false) ⇒ Object



48
49
50
51
52
53
# File 'lib/dnsruby/message/encoder.rb', line 48

def put_rr(rr, canonical=false)
  #  RFC4034 Section 6.2
  put_name(rr.name, canonical)
  put_pack('nnN', rr.type.code, rr.klass.code, rr.ttl)
  put_length16 { rr.encode_rdata(self, canonical) }
end

#put_string(d) ⇒ Object

Raises:



38
39
40
41
42
# File 'lib/dnsruby/message/encoder.rb', line 38

def put_string(d)
  raise EncodeError, "Character-string exceeds 255 octets: #{d.bytesize}" if d.bytesize > 255
  self.put_pack("C", d.bytesize)
  @data << d.b
end

#put_string_list(ds) ⇒ Object



44
45
46
# File 'lib/dnsruby/message/encoder.rb', line 44

def put_string_list(ds)
  ds.each { |d| self.put_string(d) }
end

#to_s ⇒ Object



9
10
11
# File 'lib/dnsruby/message/encoder.rb', line 9

def to_s
  @data
end