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)


68
69
70
71
72
73
# File 'lib/dnsruby/message/encoder.rb', line 68

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dnsruby/message/encoder.rb', line 53

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



21
22
23
24
25
26
27
28
# File 'lib/dnsruby/message/encoder.rb', line 21

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



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

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
# File 'lib/dnsruby/message/encoder.rb', line 17

def put_pack(template, *d)
  @data << d.pack(template)
end

#put_rr(rr, canonical = false) ⇒ Object



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

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



30
31
32
33
# File 'lib/dnsruby/message/encoder.rb', line 30

def put_string(d)
  self.put_pack("C", d.length)
  @data << d
end

#put_string_list(ds) ⇒ Object



35
36
37
# File 'lib/dnsruby/message/encoder.rb', line 35

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