Class: Dnsruby::RR::SVCB
- Inherits:
-
RR
- Object
- RR
- Dnsruby::RR::SVCB
- Defined in:
- lib/dnsruby/resource/SVCB.rb
Overview
Class for DNS Service Binding (SVCB) resource records.
RFC 9460
The presentation format is:
Name TTL Class SVCB SvcPriority TargetName SvcParams
A SvcPriority of 0 is AliasMode, where TargetName aliases the service to another name; non-zero is ServiceMode, where the SvcParams describe the endpoint.
RR.create also takes a Hash, whose :params values are in presentation format -- the text a zone file carries after the "=". #params_to_hash is the inverse of that Hash.
Direct Known Subclasses
Constant Summary collapse
- ClassValue =
:nodoc: all
nil- TypeValue =
:nodoc: all
Types::SVCB
- KEY_NAME_TO_NUM =
The registered SvcParamKey mnemonics: 0-4 and 6 from RFC 9460, 5 (ech) from RFC 9848, 7 (dohpath) from RFC 9461, 8 (ohttp) from RFC 9540 sec 4 and 10 (docpath) from RFC 9953 sec 3. Keys whose value format is not yet published as an RFC are left out, and read and write as keyNNNNN.
{ 'mandatory' => 0, 'alpn' => 1, 'no-default-alpn' => 2, 'port' => 3, 'ipv4hint' => 4, 'ech' => 5, 'ipv6hint' => 6, 'dohpath' => 7, 'ohttp' => 8, 'docpath' => 10, }.freeze
- KEY_NUM_TO_NAME =
The same table by number, for naming a key in presentation format and in error messages. A number with no mnemonic here becomes keyNNNNN.
KEY_NAME_TO_NUM.invert.freeze
- KEYS_REQUIRING_VALUE =
Keys whose presentation format requires a SvcParamValue. An empty docpath means the root path, and an unknown key may stand alone.
[0, 1, 3, 4, 5, 6, 7].freeze
- KEYS_FORBIDDING_VALUE =
Keys defined to carry no value at all: no-default-alpn and ohttp.
[2, 8].freeze
- KEYS_FORBIDDING_ESCAPES =
RFC 9460 sections 7.2, 7.3 and 8, and RFC 9848 section 3, prohibit escapes in these named parameters. Numeric keyNNNNN values still use the generic byte format.
[0, 3, 4, 5, 6].freeze
- INVALID_KEY =
RFC 9460 sec 14.3.2 reserves 65535 as an invalid SvcParamKey.
0xffff
Instance Attribute Summary collapse
-
#params ⇒ Object
The SvcParams, an ordered Hash mapping the numeric SvcParamKey to the SvcParamValue in wire format.
-
#priority ⇒ Object
The SvcPriority field (0-65535).
-
#target ⇒ Object
The TargetName field, a Dnsruby::Name.
Class Method Summary collapse
-
.create_target_name(value) ⇒ Object
:nodoc: all.
-
.decode_rdata(msg) ⇒ Object
:nodoc: all.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
RR's default comparator uses the construction-time @rdata ivar, which can be presentation text or a tuple.
-
#==(other) ⇒ Object
(also: #eql?)
RFC 3597 section 6 compares names inside newer RDATA formats as octets.
-
#data ⇒ Object
(also: #rdata)
Keep the binary RDATA interface provided for TYPE64/TYPE65 in 1.74.0.
-
#data=(bytes) ⇒ Object
(also: #rdata=)
Parse before assigning, so an invalid replacement leaves the record unchanged.
-
#encode_rdata(msg, canonical = false) ⇒ Object
:nodoc: all.
-
#from_data(data) ⇒ Object
:nodoc: all.
-
#from_hash(hash) ⇒ Object
:nodoc: all.
-
#from_string(input) ⇒ Object
:nodoc: all.
- #hash ⇒ Object
-
#params_to_hash ⇒ Object
Returns the SvcParams as a Hash mapping the mnemonic (or "keyNNNNN") to its presentation-format value, nil for a value-less key.
-
#rdata_to_string ⇒ Object
:nodoc: all.
- #rdlength ⇒ Object
-
#validate_decoded_rdata ⇒ Object
:nodoc: all.
Instance Attribute Details
#params ⇒ Object
The SvcParams, an ordered Hash mapping the numeric SvcParamKey to the SvcParamValue in wire format. See #params_to_hash for a readable view.
73 74 75 |
# File 'lib/dnsruby/resource/SVCB.rb', line 73 def params @params end |
#priority ⇒ Object
The SvcPriority field (0-65535). Zero indicates AliasMode.
60 61 62 |
# File 'lib/dnsruby/resource/SVCB.rb', line 60 def priority @priority end |
#target ⇒ Object
The TargetName field, a Dnsruby::Name.
69 70 71 |
# File 'lib/dnsruby/resource/SVCB.rb', line 69 def target @target end |
Class Method Details
.create_target_name(value) ⇒ Object
:nodoc: all
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/dnsruby/resource/SVCB.rb', line 274 def self.create_target_name(value) #:nodoc: all # IDNA conversion case-folds ASCII, but RFC 3597 sec 7 requires the # original octets in SVCB/HTTPS canonical RDATA. DNS presentation # strings (including escaped wire octets) are already ASCII; parse # them directly. Retain Name.create's IDNA handling for Unicode input # and its copying behavior for an existing Name. if value.is_a?(String) value = Name.punycode(value) unless value.ascii_only? # Only an unescaped final dot denotes the root. A literal dot in # the last label (foo\.) still needs the zone origin appended. absolute = /(?<!\\)(?:\\\\)*\.\z/.match?(value) Name.new(Name.split_escaped(value), absolute) else Name.create(value) end end |
.decode_rdata(msg) ⇒ Object
:nodoc: all
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/dnsruby/resource/SVCB.rb', line 235 def self.decode_rdata(msg) #:nodoc: all # The reading half of the empty record above. unless msg.has_remaining? record = new([nil, nil, {}]) record.instance_variable_set(:@rdata, ''.b) return record end start = msg.index priority, = msg.get_unpack('n') target = decode_target_name(msg) params = {} last_key = nil while msg.has_remaining? key, length = msg.get_unpack('nn') if last_key && key <= last_key raise DecodeError.new('SvcParams must be in strictly increasing key order without duplicates') end last_key = key # get_bytes returns what is there, so a length past the end would end # this loop short. Message.decode catches that at the RDLENGTH # boundary; RR.new_from_data, decoding RDATA alone, has none. value = msg.get_bytes(length).to_s # nil past the end of the buffer if value.bytesize != length raise DecodeError.new("SvcParamValue for #{num_to_key(key)} is truncated: " \ "#{length} octets declared, #{value.bytesize} present") end validate_param_value(key, value) params[key] = value end validate_svcparams(priority, params) record = new([priority, target, params]) # Keep the original bytes in @rdata for callers such as JSONDNS that # read it directly. The public data/rdata readers rebuild the bytes # from the current fields. record.instance_variable_set(:@rdata, msg.data.byteslice(start, msg.index - start).b) record end |
Instance Method Details
#<=>(other) ⇒ Object
RR's default comparator uses the construction-time @rdata ivar, which can be presentation text or a tuple. Compare the same current bytes that callers receive from #rdata, regardless of construction path.
102 103 104 105 106 107 108 |
# File 'lib/dnsruby/resource/SVCB.rb', line 102 def <=>(other) if @name.canonical == other.name.canonical && @type.code == other.type.code rdata <=> other.rdata else super end end |
#==(other) ⇒ Object Also known as: eql?
RFC 3597 section 6 compares names inside newer RDATA formats as octets. RR's normal equality already compares priority and params, but Name equality deliberately ignores case. Keep the usual RR behavior (including ignoring TTL), with this additional distinction. Comparing labels also works for unfinished records used by UPDATE.
115 116 117 118 |
# File 'lib/dnsruby/resource/SVCB.rb', line 115 def ==(other) super && (@target.nil? || @target.labels.map(&:string) == other.target.labels.map(&:string)) end |
#data ⇒ Object Also known as: rdata
Keep the binary RDATA interface provided for TYPE64/TYPE65 in 1.74.0. Compute bytes on demand so they reflect changes to target or params.
77 78 79 |
# File 'lib/dnsruby/resource/SVCB.rb', line 77 def data MessageEncoder.new { |encoder| encode_rdata(encoder) }.to_s end |
#data=(bytes) ⇒ Object Also known as: rdata=
Parse before assigning, so an invalid replacement leaves the record unchanged. Raw RDATA is always interpreted as octets, not UTF-8 text.
84 85 86 87 88 89 90 91 92 |
# File 'lib/dnsruby/resource/SVCB.rb', line 84 def data=(bytes) raise DecodeError, 'SVCB RDATA must be a binary String' unless bytes.is_a?(String) raise DecodeError, 'SVCB RDATA exceeds 65535 octets' if bytes.bytesize > 0xffff parsed = self.class.decode_rdata(MessageDecoder.new(bytes.b)) parsed.klass = klass if klass parsed.validate_decoded_rdata @priority, @target, @params = parsed.priority, parsed.target, parsed.params end |
#encode_rdata(msg, canonical = false) ⇒ Object
:nodoc: all
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/dnsruby/resource/SVCB.rb', line 192 def encode_rdata(msg, canonical=false) #:nodoc: all # An RRset delete (RFC 2136 sec 2.5.2) names the type with no RDATA at # all, which is what Update#delete builds; domain_name.rb does the same. if @priority.nil? && @target.nil? return if [Classes::NONE, Classes::ANY].include?(klass) end if @priority.nil? || @target.nil? raise EncodeError.new('SVCB record needs both a SvcPriority and a ' \ "TargetName, got #{@priority.inspect} and #{@target.inspect}") end # params is publicly mutable, so construction-time checks alone are # insufficient. Validate before pack('n') can truncate a wire length. params = (@params || {}).to_a length = 3 + @target.to_a.sum { |label| 1 + label.string.bytesize } begin params.each do |num, value| unless num.is_a?(Integer) && (0...INVALID_KEY).cover?(num) && value.is_a?(String) raise DecodeError.new('SvcParams must map valid numeric keys to wire-format strings') end validate_param_value(num, value) length += 4 + value.bytesize end validate_svcparams(@priority, @params || {}) rescue DecodeError => e raise EncodeError.new(e.) end if length > 0xffff raise EncodeError.new("SVCB RDATA exceeds 65535 octets: #{length}") end msg.put_pack('n', @priority) # TargetName is never compressed (RFC 9460 section 2.2). Its case is # preserved in canonical RDATA for DNSSEC (RFC 3597 section 7). msg.put_name(@target, true, false) params.sort_by(&:first).each do |num, value| msg.put_pack('n', num) msg.put_pack('n', value.bytesize) msg.put_bytes(value.b) end end |
#from_data(data) ⇒ Object
:nodoc: all
143 144 145 146 147 148 149 150 151 |
# File 'lib/dnsruby/resource/SVCB.rb', line 143 def from_data(data) #:nodoc: all # Generic constructors accepted a one-element Array of raw bytes. # Retain that form alongside the parsed tuple used by this codec. if data.length == 1 && data.first.is_a?(String) self.data = data.first else @priority, @target, @params = data end end |
#from_hash(hash) ⇒ Object
:nodoc: all
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/dnsruby/resource/SVCB.rb', line 125 def from_hash(hash) #:nodoc: all if hash.key?(:data) && hash.key?(:rdata) && hash[:data] != hash[:rdata] raise DecodeError, 'SVCB data and rdata must agree when both are supplied' end self.data = hash.fetch(:data) { hash[:rdata] } if hash.key?(:data) || hash.key?(:rdata) self.priority = hash[:priority] if hash[:priority] @target = self.class.create_target_name(hash[:target]) if hash[:target] @params ||= {} if hash[:params] @params = {} hash[:params].each do |key, value| store_param(key.to_s, value.nil? ? nil : value.to_s) end end validate_svcparams(@priority, @params) check_complete_pair end |
#from_string(input) ⇒ Object
:nodoc: all
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/dnsruby/resource/SVCB.rb', line 153 def from_string(input) #:nodoc: all # Generic.new historically accepted raw bytes. Every complete SVCB # TargetName has a NUL terminator, so try its wire form when present. # A presentation value can itself contain NUL; retain the normal text # parser if the string is not also a complete, valid wire record. if input && input.include?("\0") begin self.data = input return rescue DecodeError # Not wire-format RDATA; parse the presentation form below. end end @params = {} return if input.nil? || input.strip.empty? tokens = split_svcparams(input.strip) self.priority = tokens.shift target = tokens.shift # Name.create(nil) would raise ArgumentError, not DecodeError. raise DecodeError.new('SVCB record expects a TargetName after the SvcPriority') if target.nil? @target = self.class.create_target_name(target) tokens.each do |token| key, eq, value = token.partition('=') store_param(key, eq.empty? ? nil : value) end validate_svcparams(@priority, @params) end |
#hash ⇒ Object
121 122 123 |
# File 'lib/dnsruby/resource/SVCB.rb', line 121 def hash super ^ (@target ? @target.labels.map(&:string).hash : 0) end |
#params_to_hash ⇒ Object
Returns the SvcParams as a Hash mapping the mnemonic (or "keyNNNNN") to its presentation-format value, nil for a value-less key.
325 326 327 328 329 |
# File 'lib/dnsruby/resource/SVCB.rb', line 325 def params_to_hash sorted_params.each_with_object({}) do |(num, value), result| result[num_to_key(num)] = decode_param_value(num, value) end end |
#rdata_to_string ⇒ Object
:nodoc: all
184 185 186 187 188 189 190 |
# File 'lib/dnsruby/resource/SVCB.rb', line 184 def rdata_to_string #:nodoc: all return '' if @priority.nil? || @target.nil? params_to_hash.inject(+"#{@priority} #{@target.to_s(true)}") do |s, (name, value)| s << (value.nil? ? " #{name}" : " #{name}=#{value}") end end |
#rdlength ⇒ Object
95 96 97 |
# File 'lib/dnsruby/resource/SVCB.rb', line 95 def rdlength data.bytesize end |
#validate_decoded_rdata ⇒ Object
:nodoc: all
291 292 293 294 295 |
# File 'lib/dnsruby/resource/SVCB.rb', line 291 def validate_decoded_rdata #:nodoc: all if @priority.nil? && ![Classes::ANY, Classes::NONE].include?(klass) raise DecodeError.new('Empty SVCB RDATA is only valid for ANY/NONE UPDATE markers') end end |