Module: Net::DNS::MDNSSD::Util

Defined in:
lib/net/dns/mdns-sd.rb

Overview

Utility routines not for general use.

Class Method Summary collapse

Class Method Details

.parse_name(dnsname) ⇒ Object

Decode a DNS-SD domain name. The format is:

[<instance>.]<_service>.<_protocol>.<domain>

Examples are:

_http._tcp.local
guest._http._tcp.local
Ensemble Musique._daap._tcp.local

The <_service>.<_protocol> combined is the <type>.

Return either:

[ <domain>, <type> ]

or

[ <domain>, <type>, <instance>]

Because of the order of the return values, it can be called like:

domain, type = MDNSSD::Util.parse_name(fullname)

or

domain, type, name = MDNSSD::Util.parse_name(fullname)

If there is no name component to fullname, name will be nil.



205
206
207
208
# File 'lib/net/dns/mdns-sd.rb', line 205

def self.parse_name(dnsname)
  domain, t1, t0, name = dnsname.to_a.reverse.map {|n| n.to_s}
  [ domain, t0 + '.' + t1, name].compact
end

.parse_strings(strings) ⇒ Object

Decode TXT record strings, an array of String.

DNS-SD defines formatting conventions for them:

  • Keys must be at least one char in range (0x20-0x7E), excluding ‘=’ (0x3D), and they must be matched case-insensitively.

  • There may be no ‘=’, in which case value is nil.

  • There may be an ‘=’ with no value, in which case value is empty string, “”.

  • Anything following the ‘=’ is a value, it is not case sensitive, can be binary, and can include whitespace.

  • Discard all keys but the first.

  • Discard a string that aren’t formatting accorded to these rules.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/net/dns/mdns-sd.rb', line 221

def self.parse_strings(strings)
  h = {}

  strings.each do |kv|
    if kv.match( /^([\x20-\x3c\x3f-\x7e]+)(?:=(.*))?$/ )
      key = $1.downcase
      value = $2
      next if h.has_key? key
      h[key] = value
    end
  end

  h
end