Class: RecordStore::Record::PTR

Inherits:
RecordStore::Record show all
Defined in:
lib/record_store/record/ptr.rb

Constant Summary collapse

IPV4_LABEL_SEQUENCE_REGEX =

IPv4: 1-3 digits (0-255) followed by dots, 1-4 times

/\A(?:([0-9]|[1-9][0-9]|[1-9][0-9][0-9])\.){1,4}/
IPV6_LABEL_SEQUENCE_REGEX =

IPv6: single hex digit followed by dots, exactly 32 times

/\A(?:[0-9a-f]\.){32}/i
IN_ADDR_ARPA_SUFFIX_REGEX =
/(in-addr|ip6)\.arpa\.\z/
IPV4_FORMAT_REGEX =

Combine with suffix for full validation

Regexp.new(IPV4_LABEL_SEQUENCE_REGEX.source + 'in-addr\.arpa\.\z')
IPV6_FORMAT_REGEX =
Regexp.new(IPV6_LABEL_SEQUENCE_REGEX.source + 'ip6\.arpa\.\z')

Constants inherited from RecordStore::Record

CNAME_REGEX, FQDN_REGEX

Instance Attribute Summary collapse

Attributes inherited from RecordStore::Record

#fqdn, #id, #ttl

Instance Method Summary collapse

Methods inherited from RecordStore::Record

#==, build_from_yaml_definition, ensure_ends_with_dot, ensure_ends_without_dot, escape, #hash, #key, #log!, long_quote, needs_long_quotes?, quote, #to_hash, #to_json, #to_s, #type, unescape, unlong_quote, unquote, #wildcard?

Constructor Details

#initialize(record) ⇒ PTR

Returns a new instance of PTR.



22
23
24
25
26
# File 'lib/record_store/record/ptr.rb', line 22

def initialize(record)
  super

  @ptrdname = Record.ensure_ends_with_dot(record.fetch(:ptrdname))
end

Instance Attribute Details

#ptrdnameObject

Returns the value of attribute ptrdname.



3
4
5
# File 'lib/record_store/record/ptr.rb', line 3

def ptrdname
  @ptrdname
end

Instance Method Details

#rdataObject



28
29
30
# File 'lib/record_store/record/ptr.rb', line 28

def rdata
  { ptrdname: ptrdname }
end

#rdata_txtObject



32
33
34
# File 'lib/record_store/record/ptr.rb', line 32

def rdata_txt
  ptrdname.to_s
end

#validate_fqdn_is_in_addr_arpa_subzoneObject



55
56
57
58
59
# File 'lib/record_store/record/ptr.rb', line 55

def validate_fqdn_is_in_addr_arpa_subzone
  unless IN_ADDR_ARPA_SUFFIX_REGEX.match?(fqdn)
    errors.add(:fqdn, 'PTR records may only exist in the in-addr.arpa zone')
  end
end

#validate_fqdn_octets_in_rangeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/record_store/record/ptr.rb', line 36

def validate_fqdn_octets_in_range
  if fqdn.end_with?('in-addr.arpa.')
    # IPv4 validation: each octet must be 0-255
    IPV4_LABEL_SEQUENCE_REGEX.match(fqdn) do |m|
      unless m.captures.all? { |o| o.to_i.between?(0, 255) }
        errors.add(:fqdn, 'IPv4 octet labels must be within the range 0-255')
      end
    end
  elsif fqdn.end_with?('ip6.arpa.')
    # IPv6 validation: each nibble must be a valid hex digit
    IPV6_LABEL_SEQUENCE_REGEX.match(fqdn) do |m|
      nibbles = m[0].split('.').reject(&:empty?)
      unless nibbles.all? { |n| n.match?(/\A[0-9a-f]\z/i) }
        errors.add(:fqdn, 'IPv6 labels must be single hexadecimal digits (0-9, a-f)')
      end
    end
  end
end