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 = +''
  @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
end

#put_label(d) ⇒ Object

Raises:

  • (RuntimeError)


76
77
78
79
80
81
# File 'lib/dnsruby/message/encoder.rb', line 76

def put_label(d)
  #       s, = Name.encode(d)
  s = d
  raise RuntimeError, "length of #{s} is #{s.string.length} (larger than 63 octets)" if s.string.length > 63
  self.put_string(s.string)
end

#put_labels(d, do_canonical) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dnsruby/message/encoder.rb', line 61

def put_labels(d, do_canonical)
  d.each_index do |i|
    domain = d[i..-1].join('.')
    if !do_canonical && (idx = @names[domain])
      self.put_pack('n', 0xc000 | idx)
      return
    else
      @names[domain] = @data.length
      self.put_label(d[i])
    end
  end
  @data << "\0"
end

#put_length16Object



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

def put_length16
  length_index = @data.length
  @data << "\0\0"
  data_start = @data.length
  yield
  data_end = @data.length
  @data[length_index, 2] = [data_end - data_start].pack("n")
end

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



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

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



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

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



34
35
36
37
38
39
40
41
# File 'lib/dnsruby/message/encoder.rb', line 34

def put_string(d)
  begin
    self.put_pack("C", d.length)
    @data << d
  rescue Encoding::CompatibilityError
    raise Dnsruby::EncodeError.new("IDN support currently requires punycode string")
  end
end

#put_string_list(ds) ⇒ Object



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

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

#to_sObject



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

def to_s
  @data
end