Class: Dnsruby::RR::OPT

Inherits:
Dnsruby::RR show all
Defined in:
lib/dnsruby/resource/OPT.rb

Overview

Class for EDNS pseudo resource record OPT. This class is effectively internal to Dnsruby See RFC 2671, RFC 2435 Section 3

@TODO@ Extended labels RFC2671 section 3

Defined Under Namespace

Classes: Option

Constant Summary collapse

ClassValue =

:nodoc: all

nil
TypeValue =

:nodoc: all

Types::OPT
DO_BIT =

:nodoc: all

0x8000
ADDRESS_FAMILIES =
[1, 2]
EDNS_SUBNET_OPTION =
8

Constants inherited from Dnsruby::RR

ClassInsensitiveTypes

Instance Attribute Summary

Attributes inherited from Dnsruby::RR

#klass, #name, #rdata, #ttl, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dnsruby::RR

#<=>, #==, #clone, create, #eql?, find_class, #from_hash, get_class, get_num, #hash, implemented_rrs, #init_defaults, new_from_data, new_from_hash, new_from_string, #rdata_to_string, #rdlength, #sameRRset

Constructor Details

#initialize(*args) ⇒ OPT

Can be called with up to 3 arguments, none of which must be present

  • OPT.new()

  • OPT.new(size)

  • OPT.new(size,flags)

  • OPT.new(size,flags,options)



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

def initialize(*args)
  @type = Types.new('OPT')
  @ttl = nil

  @options=nil
  if (args.length > 0)
    self.payloadsize=(args[0])
    if (args.length > 1)
      self.flags=(args[1])
      if (args.length > 2)
        self.options=(args[2])
      else
        self.options=nil
      end
    else
      self.flags=0
    end
  else
    self.payloadsize=0
  end
end

Class Method Details

.decode_rdata(msg) ⇒ Object

:nodoc: all



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/dnsruby/resource/OPT.rb', line 250

def self.decode_rdata(msg)#:nodoc: all
  if (msg.has_remaining?)
    options = []
    while (msg.has_remaining?) do
      code  = msg.get_unpack('n')[0]
      len = msg.get_unpack('n')[0]
      data = msg.get_bytes(len)
      options.push(Option.new(code, data))
    end
  end
  return self.new(0, 0, options)
end

Instance Method Details

#dnssec_okObject



128
129
130
# File 'lib/dnsruby/resource/OPT.rb', line 128

def dnssec_ok
  return ((flags() & DO_BIT) == DO_BIT)
end

#dnssec_ok=(on) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/dnsruby/resource/OPT.rb', line 132

def dnssec_ok=(on)
  if (on)
    set_flags(flags() | DO_BIT)
  else
    set_flags(flags() & (~DO_BIT))
  end
end

#edns_client_subnetObject



219
220
221
222
223
# File 'lib/dnsruby/resource/OPT.rb', line 219

def edns_client_subnet
  return nil if @options.nil?
  subnet_option = @options.detect { |option| option.code == EDNS_SUBNET_OPTION }
  subnet_option ? get_client_subnet(subnet_option) : nil
end

#encode_rdata(msg, canonical = false) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/dnsruby/resource/OPT.rb', line 240

def encode_rdata(msg, canonical=false)
  if (@options)
    @options.each do |opt|
      msg.put_pack('n', opt.code)
      msg.put_pack('n', opt.data.length)
      msg.put_pack('a*', opt.data)
    end
  end
end

#flagsObject



116
117
118
# File 'lib/dnsruby/resource/OPT.rb', line 116

def flags
  return flags_from_ttl[2, 2].unpack("n")[0]
end

#flags=(code) ⇒ Object



120
121
122
# File 'lib/dnsruby/resource/OPT.rb', line 120

def flags=(code)
  set_flags(code)
end

#flags_from_ttlObject

4.6. The extended RCODE and flags (which OPT stores in the RR TTL field) are structured as follows:

              +0 (MSB)                            +1 (LSB)
   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0: |         EXTENDED-RCODE        |            VERSION            |
   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
2: |                               Z                               |
   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

EXTENDED-RCODE  Forms upper 8 bits of extended 12-bit RCODE.  Note
                that EXTENDED-RCODE value "0" indicates that an
                unextended RCODE is in use (values "0" through "15").

VERSION         Indicates the implementation level of whoever sets
                it.  Full conformance with this specification is
                indicated by version "0."


91
92
93
94
95
96
97
# File 'lib/dnsruby/resource/OPT.rb', line 91

def flags_from_ttl
  if (@ttl)
    return [@ttl].pack("N")
  else
    return [0].pack("N")
  end
end

#from_data(data) ⇒ Object



167
168
169
# File 'lib/dnsruby/resource/OPT.rb', line 167

def from_data(data)
  @options = data
end

#from_string(input) ⇒ Object

Raises:

  • (NotImplementedError)


171
172
173
# File 'lib/dnsruby/resource/OPT.rb', line 171

def from_string(input)
  raise NotImplementedError
end

#get_client_subnet(opt) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/dnsruby/resource/OPT.rb', line 184

def get_client_subnet(opt)
  family = opt.data[1].unpack('C')[0]
  return "Unsupported(family=#{family})" unless ADDRESS_FAMILIES.include?(family)

  source_netmask = opt.data[2].unpack('C')[0]
  scope_netmask = opt.data[3].unpack('C')[0]

  case family
  when IPV4_ADDRESS_FAMILY
    return "#{IPAddr::ntop(get_ip_addr(opt,family,source_netmask))}/#{source_netmask}/#{scope_netmask}"
  when IPV6_ADDRESS_FAMILY
    new_ipv6 = IPAddr.new(IPAddr::ntop(get_ip_addr(opt,family,source_netmask)), Socket::AF_INET6)
    return "#{new_ipv6}/#{source_netmask}/#{scope_netmask}"
  end
end

#get_ip_addr(opt, family, source_netmask) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/dnsruby/resource/OPT.rb', line 175

def get_ip_addr(opt, family, source_netmask)
  pad_format_string = family == IPV4_ADDRESS_FAMILY ? 'x3C' : 'x15C'
  ip_addr = [0].pack(pad_format_string)

  num_to_copy = (source_netmask + 7) / 8
  num_to_copy.times { |index| ip_addr[index] = opt.data[index+4] }
  ip_addr
end

#options(args) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dnsruby/resource/OPT.rb', line 148

def options(args)
  if (args==nil)
    return @options
  elsif args.kind_of?(Integer)
    #  return list of options with that code
    ret = []
    @options.each do |option|
      if (option.code == args)
        ret.push(option)
      end
    end
    return ret
  end
end

#options=(options) ⇒ Object



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

def options=(options)
  @options = options
end

#payloadsizeObject



140
141
142
# File 'lib/dnsruby/resource/OPT.rb', line 140

def payloadsize
  return @klass.code
end

#payloadsize=(size) ⇒ Object



144
145
146
# File 'lib/dnsruby/resource/OPT.rb', line 144

def payloadsize=(size)
  self.klass=Classes.new(size)
end

#set_client_subnet(subnet) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/dnsruby/resource/OPT.rb', line 200

def set_client_subnet(subnet)
  family = IPV4_ADDRESS_FAMILY
  scope_netmask = 0
  ip, source_netmask = subnet.split('/')
  source_netmask = source_netmask.to_i
  if subnet == "0.0.0.0/0"
    edns_client_subnet = RR::OPT::Option.new(
        EDNS_SUBNET_OPTION, [family, source_netmask, scope_netmask].pack("xcc*"))
  else
    ip_address = IPAddr.new(ip)
    family = IPV6_ADDRESS_FAMILY if ip_address.ipv6?
    num_addr_bytes = source_netmask / 8
    num_addr_bytes = num_addr_bytes + 1 if source_netmask % 8 > 0
    edns_client_subnet = RR::OPT::Option.new(EDNS_SUBNET_OPTION, [family, source_netmask, scope_netmask].pack("xcc*") +
      ip_address.hton.slice(0, num_addr_bytes))
  end
  self.options = [edns_client_subnet]
end

#set_flags(code) ⇒ Object

Should always be zero



124
125
126
# File 'lib/dnsruby/resource/OPT.rb', line 124

def set_flags(code) # Should always be zero
  @ttl = (xrcode().code << 24) + (version() << 16) + code
end

#to_sObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/dnsruby/resource/OPT.rb', line 225

def to_s
  ret = "OPT pseudo-record : payloadsize #{payloadsize}, xrcode #{xrcode.code}, version #{version}, flags #{flags}\n"
  if @options
    @options.each do |opt|
      if opt.code == EDNS_SUBNET_OPTION
        ret = ret + "CLIENT-SUBNET: #{get_client_subnet(opt)}"
      else
        ret = ret + " " + opt.to_s
      end
    end
  end
  ret = ret + "\n"
  return ret
end

#versionObject



108
109
110
# File 'lib/dnsruby/resource/OPT.rb', line 108

def version
  return flags_from_ttl[1, 1].unpack("C")[0]
end

#version=(code) ⇒ Object



112
113
114
# File 'lib/dnsruby/resource/OPT.rb', line 112

def version=(code)
  @ttl = (xrcode().code << 24) + (code << 16) + flags()
end

#xrcodeObject



99
100
101
# File 'lib/dnsruby/resource/OPT.rb', line 99

def xrcode
  return ExtendedRCode.new(flags_from_ttl[0, 1].unpack("C")[0])
end

#xrcode=(c) ⇒ Object



103
104
105
106
# File 'lib/dnsruby/resource/OPT.rb', line 103

def xrcode=(c)
  code = ExtendedRCode.new(c)
  @ttl = (code.code << 24) + (version() << 16) + flags()
end