class DNSSD::Record

Created when adding a DNS record using DNSSD::Service#add_record. Provides convenience methods for creating the DNS record.

See also RFC 1035

Constants

VALUE_TO_NAME

Maps record constant values to the constant name

Public Class Methods

string_to_character_string(string) click to toggle source

Turns string into an RFC-1035 character-string

# File lib/dnssd/record.rb, line 24
def self.string_to_character_string(string)
  length = string.length
  raise ArgumentError, "#{string.inspect} is too long (255 bytes max)" if
    length > 255
  "#{length.chr}#{string}"
end
string_to_domain_name(string) click to toggle source

Turns string into an RFC-1035 domain-name

# File lib/dnssd/record.rb, line 34
def self.string_to_domain_name(string)
  string.split('.').map do |part|
    string_to_character_string part
  end.join('') << "\0"
end
to_data(type, *args) click to toggle source

Encodes resource args into type. Handles:

A AAAA CNAME MX NS PTR SOA SRV TXT

# File lib/dnssd/record.rb, line 45
def self.to_data(type, *args)
  raise ArgumentError, "unknown type #{type}" unless VALUE_TO_NAME.key? type

  data = case type
         when A then
           addr = args.shift
           addr = IPAddr.new addr unless IPAddr === addr
           raise ArgumentError, "#{addr} is not IPv4" unless addr.ipv4?
           addr.hton
         when AAAA then
           addr = args.shift
           addr = IPAddr.new addr unless IPAddr === addr
           raise ArgumentError, "#{addr} is not IPv6" unless addr.ipv6?
           addr.hton
         when CNAME, NS, PTR then
           string_to_domain_name args.shift
         when MX then
           [args.shift, string_to_domain_name(args.shift)].pack 'na*'
         when SOA then
           [
             string_to_domain_name(args.shift),
             string_to_domain_name(args.shift),
             args.shift, args.shift, args.shift, args.shift, args.shift
           ].pack 'a*a*NNNNN'
         when SRV then
           [
             args.shift, args.shift, args.shift,
             string_to_domain_name(args.shift)
           ].pack 'nnna*'
         when TXT then
           data = args.map do |string|
             string_to_character_string string
           end.join ''

           raise ArgumentError,
                 "TXT record too long (#{data.length} bytes)" if
             data.length > 65535

           args.clear

           data
         else
           raise ArgumentError, "unhandled record type #{VALUE_TO_NAME[type]}"
         end

  raise ArgumentError, "Too many arguments for #{VALUE_TO_NAME[type]}" unless
    args.empty?

  data
end