File: dnsruby.rb

package info (click to toggle)
dnsruby 1.61.5-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,520 kB
  • sloc: ruby: 17,811; makefile: 3
file content (241 lines) | stat: -rw-r--r-- 6,722 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# --
# Copyright 2007 Nominet UK
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ++
require 'dnsruby/code_mappers'
require 'dnsruby/message/message'
require 'dnsruby/ipv4'
require 'dnsruby/ipv6'
require 'timeout'
require 'dnsruby/the_log'
require 'dnsruby/version'
require 'dnsruby/cache'
require 'dnsruby/DNS'
require 'dnsruby/hosts'
require 'dnsruby/update'
require 'dnsruby/zone_transfer'
require 'dnsruby/dnssec'
require 'dnsruby/zone_reader'
require 'dnsruby/resolv'


# = Dnsruby library
# Dnsruby is a thread-aware DNS stub resolver library written in Ruby.
# 
# It is based on resolv.rb, the standard Ruby DNS implementation,
# but gives a complete DNS implementation, including DNSSEC.
# 
# The Resolv class can be used to resolve addresses using /etc/hosts and /etc/resolv.conf,
# or the DNS class can be used to make DNS queries. These interfaces will attempt to apply
# the default domain and searchlist when resolving names.
# 
# The Resolver and SingleResolver interfaces allow finer control of individual messages.
# The Resolver class sends queries to multiple resolvers using various retry mechanisms.
# The SingleResolver class is used by Resolver to send individual Messages to individual
# resolvers.
# 
# Resolver queries return Dnsruby::Message objects.  Message objects have five
# sections:
# 
# * The header section, a Dnsruby::Header object.
# 
# * The question section, a list of Dnsruby::Question objects.
# 
# * The answer section, a list of Dnsruby::Resource objects.
# 
# * The authority section, a list of Dnsruby::Resource objects.
# 
# * The additional section, a list of Dnsruby::Resource objects.
# 
# 
# == example
#  res = Dnsruby::Resolver.new # System default
#  ret = res.query("example.com")
#  print "#{ret.anwer.length} answer records returned, #{ret.answer.rrsets.length} RRSets returned in aswer section\n"
# 
#  p Dnsruby::Resolv.getaddress("www.ruby-lang.org")
#  p Dnsruby::Resolv.getname("210.251.121.214")
# 
#  Dnsruby::DNS.open {|dns|
#    p dns.getresources("www.ruby-lang.org", Dnsruby::Types.A).collect {|r| r.address}
#    p dns.getresources("ruby-lang.org", 'MX').collect {|r| [r.exchange.to_s, r.preference]}
#  }
# 
# == exceptions
# 
# * ResolvError < StandardError
# 
# * ResolvTimeout < Timeout::Error
# 
# * NXDomain < ResolvError
# 
# * FormErr < ResolvError
# 
# * ServFail < ResolvError
# 
# * NotImp < ResolvError
# 
# * Refused < ResolvError
# 
# * NotZone < ResolvError
# 
# * YXDomain < ResolvError
# 
# * YXRRSet < ResolvError
# 
# * NXRRSet < ResolvError
# 
# * NotAuth < ResolvError
# 
# * OtherResolvError < ResolvError
# 
# == I/O
# Dnsruby implements a pure Ruby event loop to perform I/O.
# Support for EventMachine has been deprecated.
# 
# == DNSSEC
# Dnsruby supports DNSSEC and NSEC(3).
# DNSSEC support is on by default - but no trust anchors are configured by default.
# See Dnsruby::Dnssec for more details.
# 
# == Codes
# Dnsruby makes extensive use of several different types of codes.  These are implemented
# in the form of subclasses of CodeMapper and are located in lib/code_mappers.rb.  They are:
# 
# * OpCode - e.g. Query, Status, Notify
# * RCode - e.g. NOERROR, NXDOMAIN
# * ExtendedRCode - currently only BADVERS
# * Classes - IN, CH, HS, NONE, ANY
# * Types - RR types, e.g. A, NS, SOA
# * QTypes - IXFR, AXFR, MAILB, MAILA, ANY
# * MetaTypes - TKEY, TSIG, OPT
# * Algorithms - e.g. RSAMD5, DH, DSA
# * Nsec3HashAlgorithms - currently only SHA-1

# == Bugs
# * NIS is not supported.
# * /etc/nsswitch.conf is not supported.
# * NSEC3 validation still TBD
module Dnsruby

  def Dnsruby.version
    return VERSION
  end

  @@logger = Logger.new(STDOUT)
  @@logger.level = Logger::FATAL
  # Get the log for Dnsruby
  # Use this to set the log level
  # e.g. Dnsruby.log.level = Logger::INFO
  def Dnsruby.log
    @@logger
  end


  #  Logs (error level) and raises an error.
  def log_and_raise(object, error_class = RuntimeError)
    if object.is_a?(Exception)
      error = object
      Dnsruby.log.error(error.inspect)
      raise error
    else
      message = object.to_s
      Dnsruby.log.error(message)
      raise error_class.new(message)
    end
  end; module_function :log_and_raise

  # An error raised while querying for a resource
  class ResolvError < StandardError
    attr_accessor :response
  end

  # A timeout error raised while querying for a resource
  class ResolvTimeout < Timeout::Error
  end

  # The requested domain does not exist
  class NXDomain < ResolvError
  end

  # A format error in a received DNS message
  class FormErr < ResolvError
  end

  # Indicates a failure in the remote resolver
  class ServFail < ResolvError
  end

  # The requested operation is not implemented in the remote resolver
  class NotImp < ResolvError
  end

  # The requested operation was refused by the remote resolver
  class Refused < ResolvError
  end

  # The update RR is outside the zone (in dynamic update)
  class NotZone < ResolvError
  end

  # Some name that ought to exist, does not exist (in dynamic update)
  class YXDomain < ResolvError
  end

  # Some RRSet that ought to exist, does not exist (in dynamic update)
  class YXRRSet < ResolvError
  end

  # Some RRSet that ought not to exist, does exist (in dynamic update)
  class NXRRSet < ResolvError
  end

  # The nameserver is not responsible for the zone (in dynamic update)
  class NotAuth < ResolvError
  end


  # Another kind of resolver error has occurred
  class OtherResolvError < ResolvError
  end

  # Socket was closed by server before request was processed
  class SocketEofResolvError < ResolvError
  end

  # An error occurred processing the TSIG
  class TsigError < OtherResolvError
  end

  #  Sent a signed packet, got an unsigned response
  class TsigNotSignedResponseError < TsigError
  end

  # Indicates an error in decoding an incoming DNS message
  class DecodeError < ResolvError
    attr_accessor :partial_message
  end

  # Indicates an error encoding a DNS message for transmission
  class EncodeError < ResolvError
  end

  # Indicates an error verifying
  class VerifyError < ResolvError
  end

  # Indicates a zone transfer has failed due to SOA serial mismatch
  class ZoneSerialError < ResolvError
  end
end