Class: RubyDNS::Transaction
- Inherits:
-
Object
- Object
- RubyDNS::Transaction
- Defined in:
- lib/rubydns/transaction.rb
Overview
This class provides all details of a single DNS question and answer. This is used by the DSL to provide DNS related functionality.
Instance Attribute Summary collapse
-
#answer ⇒ Object
readonly
The current full answer to the incoming query.
-
#query ⇒ Object
readonly
The incoming query which is a set of questions.
-
#question ⇒ Object
readonly
The question that this transaction represents.
-
#resource_class ⇒ Object
readonly
The resource_class that was requested.
Instance Method Summary collapse
-
#append!(*resources) ⇒ Object
Append a given set of resources to the answer.
-
#append_query!(name, resource_type = nil) ⇒ Object
Run a new query through the rules with the given name and resource type.
-
#failure!(rcode) ⇒ Object
This function indicates that there was a failure to resolve the given question.
-
#initialize(server, query, question, resource_class, answer) ⇒ Transaction
constructor
A new instance of Transaction.
-
#name ⇒ Object
Return the name of the question, which is typically the requested hostname.
-
#passthrough!(resolver, &block) ⇒ Object
Use the given resolver to respond to the question.
- #process ⇒ Object
-
#record_type ⇒ Object
Return the type of record (eg.
A
,MX
) as aString
. -
#respond!(*data) ⇒ Object
Respond to the given query with a resource record.
-
#to_s ⇒ Object
Suitable for debugging purposes.
Constructor Details
#initialize(server, query, question, resource_class, answer) ⇒ Transaction
Returns a new instance of Transaction.
41 42 43 44 45 46 47 48 49 |
# File 'lib/rubydns/transaction.rb', line 41 def initialize(server, query, question, resource_class, answer) @server = server @query = query @question = question @resource_class = resource_class @answer = answer @question_appended = false end |
Instance Attribute Details
#answer ⇒ Object (readonly)
The current full answer to the incoming query.
62 63 64 |
# File 'lib/rubydns/transaction.rb', line 62 def answer @answer end |
#query ⇒ Object (readonly)
The incoming query which is a set of questions.
56 57 58 |
# File 'lib/rubydns/transaction.rb', line 56 def query @query end |
#question ⇒ Object (readonly)
The question that this transaction represents.
59 60 61 |
# File 'lib/rubydns/transaction.rb', line 59 def question @question end |
#resource_class ⇒ Object (readonly)
The resource_class that was requested. This is typically used to generate a response.
53 54 55 |
# File 'lib/rubydns/transaction.rb', line 53 def resource_class @resource_class end |
Instance Method Details
#append!(*resources) ⇒ Object
Append a given set of resources to the answer. The last argument can optionally be a hash of options.
options[:ttl]
-
Specify the TTL for the resource
options[:name]
-
Override the name (question) of the response.
This function can be used to supply multiple responses to a given question. For example, each argument is expected to be an instantiated resource from Resolv::DNS::Resource
module.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/rubydns/transaction.rb', line 156 def append! (*resources) append_question! = resources.last.kind_of?(Hash) ? resources.pop.dup : {} [:ttl] ||= 16000 [:name] ||= @question.to_s + "." resources.each do |resource| @answer.add_answer([:name], [:ttl], resource) end @answer.encode true end |
#append_query!(name, resource_type = nil) ⇒ Object
Run a new query through the rules with the given name and resource type. The results of this query are appended to the current transactions answer
.
81 82 83 |
# File 'lib/rubydns/transaction.rb', line 81 def append_query!(name, resource_type = nil) Transaction.new(@server, @query, name, RubyDNS.lookup_resource_class(resource_type) || @resource_class, @answer).process end |
#failure!(rcode) ⇒ Object
This function indicates that there was a failure to resolve the given question. The single argument must be an integer error code, typically given by the constants in Resolv::DNS::RCode
.
The easiest way to use this function it to simply supply a symbol. Here is a list of the most commonly used ones:
:NoError
-
No error occurred.
:FormErr
:: The incoming data was not formatted correctly.
:ServFail
-
The operation caused a server failure (internal error, etc).
:NXDomain
-
Non-eXistant Domain (domain record does not exist).
:NotImp
-
The operation requested is not implemented.
:Refused
-
The operation was refused by the server.
:NotAuth
-
The server is not authoritive for the zone.
See www.rfc-editor.org/rfc/rfc2929.txt for more information about DNS error codes (specifically, page 3).
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/rubydns/transaction.rb', line 189 def failure! (rcode) append_question! if rcode.kind_of? Symbol @answer.rcode = Resolv::DNS::RCode.const_get(rcode) else @answer.rcode = rcode.to_i end true end |
#name ⇒ Object
Return the name of the question, which is typically the requested hostname.
70 71 72 |
# File 'lib/rubydns/transaction.rb', line 70 def name @question.to_s end |
#passthrough!(resolver, &block) ⇒ Object
Use the given resolver to respond to the question. This will query
the resolver and merge!
the answer if one is received. If recursion is not requested, the result is failure!(:Refused)
. If the resolver does not respond, the result is failure!(:NXDomain)
If a block is supplied, this function yields with the reply and reply_name if successful. This could be used, for example, to update a cache.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rubydns/transaction.rb', line 96 def passthrough! (resolver, &block) # Were we asked to recursively find this name? if @query.rd reply, reply_name = resolver.query(name, resource_class) if reply if block_given? yield(reply, reply_name) end @answer.merge!(reply) else failure!(:NXDomain) end else failure!(:Refused) end true end |
#process ⇒ Object
85 86 87 |
# File 'lib/rubydns/transaction.rb', line 85 def process @server.process(name, record_type, self) end |
#record_type ⇒ Object
Return the type of record (eg. A
, MX
) as a String
.
65 66 67 |
# File 'lib/rubydns/transaction.rb', line 65 def record_type @resource_class.name.split("::").last end |
#respond!(*data) ⇒ Object
Respond to the given query with a resource record. The arguments to this function depend on the resource_class
requested. The last argument can optionally be a hash of options.
options[:resource_class]
-
Override the default
resource_class
options[:ttl]
-
Specify the TTL for the resource
options[:name]
-
Override the name (question) of the response.
- for A records
-
respond!("1.2.3.4")
- for MX records
-
respond!("mail.blah.com", 10)
This function instantiates the resource class with the supplied arguments, and then passes it to append!
.
See Resolv::DNS::Resource
for more information about the various resource_class
s available. www.ruby-doc.org/stdlib/libdoc/resolv/rdoc/index.html
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rubydns/transaction.rb', line 134 def respond! (*data) = data.last.kind_of?(Hash) ? data.pop : {} case [:resource_class] when nil append!(@resource_class.new(*data), ) when Class append!([:resource_class].new(*data), ) else raise ArgumentError, "Could not instantiate resource!" end end |
#to_s ⇒ Object
Suitable for debugging purposes
75 76 77 |
# File 'lib/rubydns/transaction.rb', line 75 def to_s "#{name} #{record_type}" end |