Class: SvcbRrPatch::SvcParams::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/svcb_rr_patch/svc_params.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Hash

Returns a new instance of Hash.



33
34
35
# File 'lib/svcb_rr_patch/svc_params.rb', line 33

def initialize(hash)
  @hash = hash
end

Class Method Details

.decode(octet) ⇒ Hash

Parameters:

  • octet (String)

Returns:

Raises:

  • (::Resolv::DNS::DecodeError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/svcb_rr_patch/svc_params.rb', line 51

def self.decode(octet)
  svc_params = {}
  i = 0
  while i < octet.length
    raise ::Resolv::DNS::DecodeError if i + 4 > octet.length

    k = octet.slice(i, 2).unpack1('n')
    # SvcParamKeys SHALL appear in increasing numeric order.
    raise ::Resolv::DNS::DecodeError unless svc_params.keys.find do |key|
      SvcbRrPatch::SvcParams::PARAMETER_REGISTRY_INVERT[key] >= k
    end.nil?

    i += 2
    vlen = octet.slice(i, 2).unpack1('n')
    i += 2
    raise ::Resolv::DNS::DecodeError if i + vlen > octet.length

    v = octet.slice(i, vlen)
    i += vlen
    # Values are in a format specific to the SvcParamKey.
    svc_param_key = SvcbRrPatch::SvcParams::PARAMETER_REGISTRY[k]
    svc_param_values = decode_svc_params(svc_param_key, v)
    svc_params.store(svc_param_key, svc_param_values)
  end
  raise ::Resolv::DNS::DecodeError if i != octet.length

  new(svc_params)
end

.decode_svc_params(key, octet) ⇒ Object

:nodoc: rubocop:disable Metrics/CyclomaticComplexity



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/svcb_rr_patch/svc_params.rb', line 82

def self.decode_svc_params(key, octet)
  case key
  when 'mandatory'
    SvcbRrPatch::SvcParams::Mandatory.decode(octet)
  when 'alpn'
    SvcbRrPatch::SvcParams::Alpn.decode(octet)
  when 'no-default-alpn'
    SvcbRrPatch::SvcParams::NoDefaultAlpn.decode(octet)
  when 'port'
    SvcbRrPatch::SvcParams::Port.decode(octet)
  when 'ipv4hint'
    SvcbRrPatch::SvcParams::Ipv4hint.decode(octet)
  when 'ech'
    SvcbRrPatch::SvcParams::Ech.decode(octet)
  when 'ipv6hint'
    SvcbRrPatch::SvcParams::Ipv6hint.decode(octet)
  else
    octet
  end
end

Instance Method Details

#[](key) ⇒ Object

:nodoc:



105
106
107
# File 'lib/svcb_rr_patch/svc_params.rb', line 105

def [](key)
  @hash[key]
end

#encodeString

Returns:

  • (String)


38
39
40
41
42
43
44
45
46
# File 'lib/svcb_rr_patch/svc_params.rb', line 38

def encode
  @hash
    .map { |k, v| [SvcbRrPatch::SvcParams::PARAMETER_REGISTRY_INVERT[k], v] }
    .sort { |lh, rh| lh.first <=> rh.first }
    .map do |k, v|
      [k].pack('n') + v.encode.then { |s| [s.length].pack('n') + s }
    end
    .join
end

#keysObject

:nodoc:



110
111
112
# File 'lib/svcb_rr_patch/svc_params.rb', line 110

def keys
  @hash.keys
end

#to_sObject

:nodoc:



115
116
117
# File 'lib/svcb_rr_patch/svc_params.rb', line 115

def to_s
  @hash.map { |k, v| "#{k}=#{v}" }.join(' ')
end