Module: Dnsruby::RR::NXT::NxtTypes

Defined in:
lib/dnsruby/resource/NXT.rb

Overview

Methods used to manipulate the storage and representation of record types as stored in NXT record bitmaps.

Constant Summary collapse

MAX_BITMAP_NUMBER_VALUE =

Maximum bitmap size is 128 bytes; since it’s zero offset values are 0..(2 ** 128 - 1). However, the least significant bit must not be set, so the maximum is 1 less than that.

(2 ** 128) - 1 - 1

Class Method Summary collapse

Class Method Details

Assert that the specified number is a legal value with which to instantiate a NXT type bitmap. Raise on error, do nothing on success.



238
239
240
241
242
243
244
245
246
# File 'lib/dnsruby/resource/NXT.rb', line 238

def assert_legal_bitmap_value(number)
  max_value = NxtTypes::MAX_BITMAP_NUMBER_VALUE
  if number > max_value
    raise ArgumentError.new("Bitmap maximum value is #{max_value} (0x#{max_value.to_s(16)}).")
  end
  if number & 1 == 1
    raise ArgumentError.new("Bitmap number must not have low bit set.")
  end
end

.binary_string_to_codes(binary_string) ⇒ Object

From a binary string of type code bits, return an array of type codes.



213
214
215
216
217
# File 'lib/dnsruby/resource/NXT.rb', line 213

def binary_string_to_codes(binary_string)
  bitmap_number = BitMapping.binary_string_to_number(binary_string)
  assert_legal_bitmap_value(bitmap_number)
  BitMapping.number_to_set_bit_positions_array(bitmap_number)
end

.binary_string_to_names(binary_string) ⇒ Object

From a binary string of type code bits, return an array of type names.



221
222
223
224
# File 'lib/dnsruby/resource/NXT.rb', line 221

def binary_string_to_names(binary_string)
  codes = binary_string_to_codes(binary_string)
  codes_to_names(codes)
end

.code_to_name(number) ⇒ Object

Convert a numeric type code to its corresponding name (e.g. “A” => 1). Unknown types are named “TYPE#number”.



172
173
174
# File 'lib/dnsruby/resource/NXT.rb', line 172

def code_to_name(number)
  Types.to_string(number) || "TYPE#{number}"
end

.codes_to_binary_string(codes) ⇒ Object

From an array of type codes, return a binary string.



227
228
229
230
231
232
233
234
# File 'lib/dnsruby/resource/NXT.rb', line 227

def codes_to_binary_string(codes)
  codes = codes.sort
  unless legal_code_value?(codes.first) && legal_code_value?(codes.last)
    raise ArgumentError.new("All codes must be between 1 and 127: #{codes.inspect}.")
  end
  bitmap_number = BitMapping.set_bit_position_array_to_number(codes)
  BitMapping.number_to_binary_string(bitmap_number)
end

.codes_to_names(codes) ⇒ Object

For the given array of type codes, return an array of their corresponding names.



201
202
203
# File 'lib/dnsruby/resource/NXT.rb', line 201

def codes_to_names(codes)
  codes.map { |code| code_to_name(code) }
end

.codes_to_string(codes) ⇒ Object

Generate a string containing the names corresponding to the numeric type codes. Sort it by the numeric type code, ascending.



207
208
209
# File 'lib/dnsruby/resource/NXT.rb', line 207

def codes_to_string(codes)
  codes.sort.map { |code| code_to_name(code) }.join(' ')
end

Returns:

  • (Boolean)


248
249
250
# File 'lib/dnsruby/resource/NXT.rb', line 248

def legal_code_value?(code)
  (1..127).include?(code)
end

.name_to_code(name) ⇒ Object

Convert a type name to its corresponding numeric type code. Names matching /^TYPE(d+)$/ are assumed to have a code corresponding to the numeric value of the substring following ‘TYPE’.



179
180
181
182
183
184
185
186
# File 'lib/dnsruby/resource/NXT.rb', line 179

def name_to_code(name)
  code = Types.to_code(name)
  if code.nil?
    matches = /^TYPE(\d+)$/.match(name)
    code = matches[1].to_i if matches
  end
  code
end

.names_string_to_codes(name_string) ⇒ Object

For the specified string containing names (e.g. ‘A NS’), return an array containing the corresponding codes.



195
196
197
# File 'lib/dnsruby/resource/NXT.rb', line 195

def names_string_to_codes(name_string)
  names_to_codes(name_string.split(' '))
end

.names_to_codes(names) ⇒ Object

For a given array of type names, return an array of codes.



189
190
191
# File 'lib/dnsruby/resource/NXT.rb', line 189

def names_to_codes(names)
  names.map { |s| name_to_code(s) }
end