class DNSSD::Reply::QueryRecord

Created by DNSSD::Service#query_record

Attributes

domain[R]

A domain for registration or browsing

name[R]

The service name

record[R]

DNS Record data

record_class[R]

DNS Record class (only IN is supported)

record_type[R]

DNS Record type

ttl[R]

Time-to-live for this record. See expired?

type[R]

The service type

Public Class Methods

new(service, flags, interface, fullname, record_type, record_class, record, ttl) click to toggle source

Creates a new QueryRecord, called internally by DNSSD::Service#query_record

Calls superclass method DNSSD::Reply.new
# File lib/dnssd/reply/query_record.rb, line 47
def initialize(service, flags, interface, fullname, record_type,
               record_class, record, ttl)
  super service, flags, interface

  set_fullname fullname

  @record_type = record_type
  @record_class = record_class
  @record = record

  @created = Time.now
  @ttl = ttl
end

Public Instance Methods

character_string_to_string(character_string) click to toggle source

Converts a RFC 1035 character-string into a ruby String

# File lib/dnssd/reply/query_record.rb, line 64
def character_string_to_string(character_string)
  length = character_string.slice 0
  length = length.ord unless Numeric === length
  string = character_string.slice 1, length

  if string.length != length then
    raise TypeError,
      "invalid character string, expected #{length} got #{string.length} in #{@record.inspect}"
  end

  string
end
domain_name_to_string(domain_name) click to toggle source

Converts a RFC 1035 domain-name into a ruby String

# File lib/dnssd/reply/query_record.rb, line 80
def domain_name_to_string(domain_name)
  return '.' if domain_name == "\0"

  domain_name = domain_name.dup
  string = []

  until domain_name.empty? do
    string << character_string_to_string(domain_name)
    domain_name.slice! 0, string.last.length + 1
  end

  string << nil unless string.last.empty?

  string.join('.')
end
expired?() click to toggle source

Has this QueryRecord passed its TTL?

# File lib/dnssd/reply/query_record.rb, line 99
def expired?
  Time.now > @created + ttl
end
record_class_name() click to toggle source

Name of this record's #record_class

# File lib/dnssd/reply/query_record.rb, line 114
def record_class_name
  return "unknown #{@record_class}" unless @record_class == DNSSD::Record::IN
  'IN' # Only IN is supported
end
record_data() click to toggle source

Decodes output for record, returning the raw record if it can't be decoded. Handles:

A AAAA CNAME MX NS PTR SOA SRV TXT

# File lib/dnssd/reply/query_record.rb, line 125
def record_data
  return @record unless @record_class == DNSSD::Record::IN

  case @record_type
  when DNSSD::Record::A,
       DNSSD::Record::AAAA then
    IPAddr.new_ntoh @record
  when DNSSD::Record::CNAME,
       DNSSD::Record::NS,
       DNSSD::Record::PTR then
    domain_name_to_string @record
  when DNSSD::Record::MX then
    mx = @record.unpack 'nZ*'
    mx[-1] = domain_name_to_string mx.last
    mx
  when DNSSD::Record::SOA then
    soa = @record.unpack 'Z*Z*NNNNN'
    soa[0] = domain_name_to_string soa[0]
    soa[1] = domain_name_to_string soa[1]
    soa
  when DNSSD::Record::SRV then
    srv = @record.unpack 'nnnZ*'
    srv[-1] = domain_name_to_string srv.last
    srv
  when DNSSD::Record::TXT then
    record = @record.dup
    txt = []

    until record.empty? do
      txt << character_string_to_string(record)
      record.slice! 0, txt.last.length + 1
    end

    txt
  else
    @record
  end
end
record_type_name() click to toggle source

Name of this record's #record_type

# File lib/dnssd/reply/query_record.rb, line 167
def record_type_name
  return "unknown #{@record_type} for record class (#{@record_class})" unless
    @record_class == DNSSD::Record::IN
  DNSSD::Record::VALUE_TO_NAME[@record_type]
end
to_s() click to toggle source

Outputs this record in a BIND-like DNS format

# File lib/dnssd/reply/query_record.rb, line 176
def to_s
  "%s %d %s %s %p" % [
    fullname, ttl, record_class_name, record_type_name, record_data
  ]
end