Class: DNSSD::Reply::QueryRecord

Inherits:
DNSSD::Reply show all
Defined in:
lib/dnssd/reply/query_record.rb

Overview

Created by DNSSD::Service#query_record

Instance Attribute Summary collapse

Attributes inherited from DNSSD::Reply

#flags, #interface, #service

Instance Method Summary collapse

Methods inherited from DNSSD::Reply

#fullname, #interface_name, #protocol, #service_name, #set_fullname, #set_names

Constructor Details

#initialize(service, flags, interface, fullname, record_type, record_class, record, ttl) ⇒ QueryRecord

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



47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

Instance Attribute Details

#domainObject (readonly)

A domain for registration or browsing



11
12
13
# File 'lib/dnssd/reply/query_record.rb', line 11

def domain
  @domain
end

#nameObject (readonly)

The service name



16
17
18
# File 'lib/dnssd/reply/query_record.rb', line 16

def name
  @name
end

#recordObject (readonly)

DNS Record data



21
22
23
# File 'lib/dnssd/reply/query_record.rb', line 21

def record
  @record
end

#record_classObject (readonly)

DNS Record class (only IN is supported)



26
27
28
# File 'lib/dnssd/reply/query_record.rb', line 26

def record_class
  @record_class
end

#record_typeObject (readonly)

DNS Record type



31
32
33
# File 'lib/dnssd/reply/query_record.rb', line 31

def record_type
  @record_type
end

#ttlObject (readonly)

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



36
37
38
# File 'lib/dnssd/reply/query_record.rb', line 36

def ttl
  @ttl
end

#typeObject (readonly)

The service type



41
42
43
# File 'lib/dnssd/reply/query_record.rb', line 41

def type
  @type
end

Instance Method Details

#character_string_to_string(character_string) ⇒ Object

Converts a RFC 1035 character-string into a ruby String



64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object

Converts a RFC 1035 domain-name into a ruby String



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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?Boolean

Has this QueryRecord passed its TTL?

Returns:

  • (Boolean)


99
100
101
# File 'lib/dnssd/reply/query_record.rb', line 99

def expired?
  Time.now > @created + ttl
end

#inspectObject

:nodoc:



103
104
105
106
107
108
109
# File 'lib/dnssd/reply/query_record.rb', line 103

def inspect # :nodoc:
  "#<%s:0x%x %s %s %s %p interface: %s flags: %p>" % [
    self.class, object_id,
    fullname, record_class_name, record_type_name, record,
    interface_name, @flags
  ]
end

#record_class_nameObject

Name of this record’s record_class



114
115
116
117
# 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_dataObject

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

A AAAA CNAME MX NS PTR SOA SRV TXT



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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_nameObject

Name of this record’s record_type



167
168
169
170
171
# 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_sObject

Outputs this record in a BIND-like DNS format



176
177
178
179
180
# 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