PageRenderTime 85ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Dnsruby-1.0/dnsruby.rb

https://github.com/adamwiggins/whatswrong
Ruby | 488 lines | 292 code | 65 blank | 131 comment | 27 complexity | caaed180e07c06ae67da77808d7010f7 MD5 | raw file
  1. #--
  2. #Copyright 2007 Nominet UK
  3. #
  4. #Licensed under the Apache License, Version 2.0 (the "License");
  5. #you may not use this file except in compliance with the License.
  6. #You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. #Unless required by applicable law or agreed to in writing, software
  11. #distributed under the License is distributed on an "AS IS" BASIS,
  12. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. #See the License for the specific language governing permissions and
  14. #limitations under the License.
  15. #++
  16. require 'Dnsruby/TheLog'
  17. require 'Dnsruby/code_mapper'
  18. require 'Dnsruby/ipv4'
  19. require 'Dnsruby/ipv6'
  20. require 'timeout'
  21. #= Dnsruby library
  22. #Dnsruby is a thread-aware DNS stub resolver library written in Ruby.
  23. #
  24. #It is based on resolv.rb, the standard Ruby DNS implementation,
  25. #but gives a complete DNS implementation complying with all relevant
  26. #RFCs.
  27. #
  28. #The Resolv class can be used to resolve addresses using /etc/hosts and /etc/resolv.conf,
  29. #or the DNS class can be used to make DNS queries. These interfaces will attempt to apply
  30. #the default domain and searchlist when resolving names.
  31. #
  32. #The Resolver and SingleResolver interfaces allow finer control of individual messages.
  33. #The Resolver class sends queries to multiple resolvers using various retry mechanisms.
  34. #The SingleResolver class is used by Resolver to send individual Messages to individual
  35. #resolvers.
  36. #
  37. #Resolver queries return Dnsruby::Message objects. Message objects have five
  38. #sections:
  39. #
  40. #* The header section, a Dnsruby::Header object.
  41. #
  42. #* The question section, a list of Dnsruby::Question objects.
  43. #
  44. #* The answer section, a list of Dnsruby::Resource objects.
  45. #
  46. #* The authority section, a list of Dnsruby::Resource objects.
  47. #
  48. #* The additional section, a list of Dnsruby::Resource objects.
  49. #
  50. #
  51. #== example
  52. # p Dnsruby::Resolv.getaddress("www.ruby-lang.org")
  53. # p Dnsruby::Resolv.getname("210.251.121.214")
  54. #
  55. # Dnsruby::DNS.open {|dns|
  56. # p dns.getresources("www.ruby-lang.org", Dnsruby::Types.A).collect {|r| r.address}
  57. # p dns.getresources("ruby-lang.org", 'MX').collect {|r| [r.exchange.to_s, r.preference]}
  58. # }
  59. #
  60. #== exceptions
  61. #
  62. #* ResolvError < StandardError
  63. #
  64. #* ResolvTimeout < TimeoutError
  65. #
  66. #* NXDomain < ResolvError
  67. #
  68. #* FormErr < ResolvError
  69. #
  70. #* ServFail < ResolvError
  71. #
  72. #* NotImp < ResolvError
  73. #
  74. #* Refused < ResolvError
  75. #
  76. #* OtherResolvError < ResolvError
  77. #
  78. #== I/O
  79. #Dnsruby implements a pure Ruby event loop to perform I/O.
  80. #Dnsruby can also use EventMachine to perform I/O, if it is available on the
  81. #platform - but it must be enabled through the Dnsruby::Resolver class
  82. #
  83. #== Bugs
  84. #* NIS is not supported.
  85. #* /etc/nsswitch.conf is not supported.
  86. #* IPv6 is not supported.
  87. module Dnsruby
  88. class OpCode < CodeMapper
  89. Query = 0 # RFC 1035
  90. IQuery = 1 # RFC 1035
  91. Status = 2 # RFC 1035
  92. Notify = 4 # RFC 1996
  93. Update = 5 # RFC 2136
  94. update()
  95. end
  96. class RCode < CodeMapper
  97. NOERROR = 0 # RFC 1035
  98. FORMERR = 1 # RFC 1035
  99. SERVFAIL = 2 # RFC 1035
  100. NXDOMAIN = 3 # RFC 1035
  101. NOTIMP = 4 # RFC 1035
  102. REFUSED = 5 # RFC 1035
  103. YXDOMAIN = 6 # RFC 2136
  104. YXRRSET = 7 # RFC 2136
  105. NXRRSET = 8 # RFC 2136
  106. NOTAUTH = 9 # RFC 2136
  107. NOTZONE = 10 # RFC 2136
  108. BADVERS = 16
  109. BADSIG = 16
  110. BADKEY = 17
  111. BADTIME = 18
  112. BADMODE = 19
  113. BADNAME = 20
  114. BADALG = 21
  115. update()
  116. end
  117. class Classes < CodeMapper
  118. IN = 1 # RFC 1035
  119. CH = 3 # RFC 1035
  120. # CHAOS = 3 # RFC 1035
  121. HS = 4 # RFC 1035
  122. # HESIOD = 4 # RFC 1035
  123. NONE = 254 # RFC 2136
  124. ANY = 255 # RFC 1035
  125. update()
  126. def unknown_string(arg)
  127. if (arg=~/^CLASS/i)
  128. Classes.add_pair(arg, arg.gsub('CLASS', '').to_i)
  129. set_string(arg)
  130. else
  131. raise ArgumentError.new("String #{arg} not a member of #{self.class}")
  132. end
  133. end
  134. def unknown_code(arg)
  135. Classes.add_pair('CLASS' + arg.to_s, arg)
  136. set_code(arg)
  137. end
  138. # classesbyval and classesbyname functions are wrappers around the
  139. # similarly named hashes. They are used for 'unknown' DNS RR classess
  140. # (RFC3597)
  141. # See typesbyval and typesbyname, these beasts have the same functionality
  142. def Classes.classesbyname(name) #:nodoc: all
  143. name.upcase!;
  144. if to_code(name)
  145. return to_code(name)
  146. end
  147. if ((name =~/^\s*CLASS(\d+)\s*$/o) == nil)
  148. raise ArgumentError, "classesbyval() argument is not CLASS### (#{name})"
  149. end
  150. val = $1.to_i
  151. if val > 0xffff
  152. raise ArgumentError, 'classesbyval() argument larger than ' + 0xffff
  153. end
  154. return val;
  155. end
  156. def Classes.classesbyval(val) #:nodoc: all
  157. if (val.class == String)
  158. if ((val =~ /^\s*0*([0-9]+)\s*$/) == nil)
  159. raise ArgumentError, "classesbybal() argument is not numeric (#{val})" # unless val.gsub!("^\s*0*([0-9]+)\s*$", "$1")
  160. # val =~ s/^\s*0*([0-9]+)\s*$/$1/o;#
  161. end
  162. val = $1.to_i
  163. end
  164. return to_string(val) if to_string(val)
  165. raise ArgumentError, 'classesbyval() argument larger than ' + 0xffff if val > 0xffff;
  166. return "CLASS#{val}";
  167. end
  168. end
  169. # The RR types explicitly supported by Dnsruby.
  170. #
  171. # New RR types should be added to this set
  172. class Types < CodeMapper
  173. SIGZERO = 0 # RFC2931 consider this a pseudo type
  174. A = 1 # RFC 1035, Section 3.4.1
  175. NS = 2 # RFC 1035, Section 3.3.11
  176. MD = 3 # RFC 1035, Section 3.3.4 (obsolete)
  177. MF = 4 # RFC 1035, Section 3.3.5 (obsolete)
  178. CNAME = 5 # RFC 1035, Section 3.3.1
  179. SOA = 6 # RFC 1035, Section 3.3.13
  180. MB = 7 # RFC 1035, Section 3.3.3
  181. MG = 8 # RFC 1035, Section 3.3.6
  182. MR = 9 # RFC 1035, Section 3.3.8
  183. NULL = 10 # RFC 1035, Section 3.3.10
  184. WKS = 11 # RFC 1035, Section 3.4.2 (deprecated)
  185. PTR = 12 # RFC 1035, Section 3.3.12
  186. HINFO = 13 # RFC 1035, Section 3.3.2
  187. MINFO = 14 # RFC 1035, Section 3.3.7
  188. MX = 15 # RFC 1035, Section 3.3.9
  189. TXT = 16 # RFC 1035, Section 3.3.14
  190. RP = 17 # RFC 1183, Section 2.2
  191. AFSDB = 18 # RFC 1183, Section 1
  192. X25 = 19 # RFC 1183, Section 3.1
  193. ISDN = 20 # RFC 1183, Section 3.2
  194. RT = 21 # RFC 1183, Section 3.3
  195. NSAP = 22 # RFC 1706, Section 5
  196. NSAP_PTR = 23 # RFC 1348 (obsolete)
  197. SIG = 24 # RFC 2535, Section 4.1
  198. KEY = 25 # RFC 2535, Section 3.1
  199. PX = 26 # RFC 2163,
  200. GPOS = 27 # RFC 1712 (obsolete)
  201. AAAA = 28 # RFC 1886, Section 2.1
  202. LOC = 29 # RFC 1876
  203. NXT = 30 # RFC 2535, Section 5.2 obsoleted by RFC3755
  204. EID = 31 # draft-ietf-nimrod-dns-xx.txt
  205. NIMLOC = 32 # draft-ietf-nimrod-dns-xx.txt
  206. SRV = 33 # RFC 2052
  207. ATMA = 34 # ???
  208. NAPTR = 35 # RFC 2168
  209. KX = 36 # RFC 2230
  210. CERT = 37 # RFC 2538
  211. DNAME = 39 # RFC 2672
  212. OPT = 41 # RFC 2671
  213. DS = 43 # RFC 4034
  214. SSHFP = 44 # draft-ietf-secsh-dns (No RFC yet at time of coding)
  215. IPSECKEY = 45 # RFC 4025
  216. RRSIG = 46 # RFC 4034
  217. NSEC = 47 # RFC 4034
  218. DNSKEY = 48 # RFC 4034
  219. DHCID = 49 # RFC 4701
  220. SPF = 99 # rfc-schlitt-spf-classic-o2 (No RFC yet at time of coding)
  221. UINFO = 100 # non-standard
  222. UID = 101 # non-standard
  223. GID = 102 # non-standard
  224. UNSPEC = 103 # non-standard
  225. TKEY = 249 # RFC 2930
  226. TSIG = 250 # RFC 2931
  227. IXFR = 251 # RFC 1995
  228. AXFR = 252 # RFC 1035
  229. MAILB = 253 # RFC 1035 (MB, MG, MR)
  230. MAILA = 254 # RFC 1035 (obsolete - see MX)
  231. ANY = 255 # RFC 1035
  232. update()
  233. def unknown_string(arg) #:nodoc: all
  234. if (arg=~/^TYPE/i)
  235. Types.add_pair(arg, arg.gsub('TYPE', '').to_i)
  236. set_string(arg)
  237. else
  238. raise ArgumentError.new("String #{arg} not a member of #{self.class}")
  239. end
  240. end
  241. def unknown_code(arg) #:nodoc: all
  242. Types.add_pair('TYPE' + arg.to_s, arg)
  243. set_code(arg)
  244. end
  245. #--
  246. # typesbyval and typesbyname functions are wrappers around the similarly named
  247. # hashes. They are used for 'unknown' DNS RR types (RFC3597)
  248. # typesbyname returns they TYPEcode as a function of the TYPE
  249. # mnemonic. If the TYPE mapping is not specified the generic mnemonic
  250. # TYPE### is returned.
  251. def Types.typesbyname(name) #:nodoc: all
  252. name.upcase!
  253. if to_code(name)
  254. return to_code(name)
  255. end
  256. if ((name =~/^\s*TYPE(\d+)\s*$/o)==nil)
  257. raise ArgumentError, "Net::DNS::typesbyname() argument (#{name}) is not TYPE###"
  258. end
  259. val = $1.to_i
  260. if val > 0xffff
  261. raise ArgumentError, 'Net::DNS::typesbyname() argument larger than ' + 0xffff
  262. end
  263. return val;
  264. end
  265. # typesbyval returns they TYPE mnemonic as a function of the TYPE
  266. # code. If the TYPE mapping is not specified the generic mnemonic
  267. # TYPE### is returned.
  268. def Types.typesbyval(val) #:nodoc: all
  269. if (!defined?val)
  270. raise ArgumentError, "Net::DNS::typesbyval() argument is not defined"
  271. end
  272. if val.class == String
  273. # if val.gsub!("^\s*0*(\d+)\s*$", "$1")
  274. if ((val =~ /^\s*0*(\d+)\s*$", "$1/o) == nil)
  275. raise ArgumentError, "Net::DNS::typesbyval() argument (#{val}) is not numeric"
  276. # val =~s/^\s*0*(\d+)\s*$/$1/o;
  277. end
  278. val = $1.to_i
  279. end
  280. if to_string(val)
  281. return to_string(val)
  282. end
  283. raise ArgumentError, 'Net::DNS::typesbyval() argument larger than ' + 0xffff if
  284. val > 0xffff;
  285. return "TYPE#{val}";
  286. end
  287. end
  288. class QTypes < CodeMapper
  289. IXFR = 251 # incremental transfer [RFC1995]
  290. AXFR = 252 # transfer of an entire zone [RFC1035]
  291. MAILB = 253 # mailbox-related RRs (MB, MG or MR) [RFC1035]
  292. MAILA = 254 # mail agent RRs (Obsolete - see MX) [RFC1035]
  293. ANY = 255 # all records [RFC1035]
  294. update()
  295. end
  296. class MetaTypes < CodeMapper
  297. TKEY = 249 # Transaction Key [RFC2930]
  298. TSIG = 250 # Transaction Signature [RFC2845]
  299. OPT = 41 # RFC 2671
  300. end
  301. #An error raised while querying for a resource
  302. class ResolvError < StandardError
  303. end
  304. #A timeout error raised while querying for a resource
  305. class ResolvTimeout < TimeoutError
  306. end
  307. #The requested domain does not exist
  308. class NXDomain < ResolvError
  309. end
  310. #A format error in a received DNS message
  311. class FormErr < ResolvError
  312. end
  313. #Indicates a failure in the remote resolver
  314. class ServFail < ResolvError
  315. end
  316. #The requested operation is not implemented in the remote resolver
  317. class NotImp < ResolvError
  318. end
  319. #The requested operation was refused by the remote resolver
  320. class Refused < ResolvError
  321. end
  322. #Another kind of resolver error has occurred
  323. class OtherResolvError < ResolvError
  324. end
  325. #Indicates an error in decoding an incoming DNS message
  326. class DecodeError < StandardError
  327. end
  328. #Indicates an error encoding a DNS message for transmission
  329. class EncodeError < StandardError
  330. end
  331. #The Resolv class can be used to resolve addresses using /etc/hosts and /etc/resolv.conf,
  332. #
  333. #The DNS class may be used to perform more queries. If greater control over the sending
  334. #of packets is required, then the Resolver or SingleResolver classes may be used.
  335. class Resolv
  336. #Looks up the first IP address for +name+
  337. def self.getaddress(name)
  338. DefaultResolver.getaddress(name)
  339. end
  340. #Looks up all IP addresses for +name+
  341. def self.getaddresses(name)
  342. DefaultResolver.getaddresses(name)
  343. end
  344. #Iterates over all IP addresses for +name+
  345. def self.each_address(name, &block)
  346. DefaultResolver.each_address(name, &block)
  347. end
  348. #Looks up the first hostname of +address+
  349. def self.getname(address)
  350. DefaultResolver.getname(address)
  351. end
  352. #Looks up all hostnames of +address+
  353. def self.getnames(address)
  354. DefaultResolver.getnames(address)
  355. end
  356. #Iterates over all hostnames of +address+
  357. def self.each_name(address, &proc)
  358. DefaultResolver.each_name(address, &proc)
  359. end
  360. #Creates a new Resolv using +resolvers+
  361. def initialize(resolvers=[Hosts.new, DNS.new])
  362. @resolvers = resolvers
  363. end
  364. #Looks up the first IP address for +name+
  365. def getaddress(name)
  366. each_address(name) {|address| return address}
  367. raise ResolvError.new("no address for #{name}")
  368. end
  369. #Looks up all IP addresses for +name+
  370. def getaddresses(name)
  371. ret = []
  372. each_address(name) {|address| ret << address}
  373. return ret
  374. end
  375. #Iterates over all IP addresses for +name+
  376. def each_address(name)
  377. if AddressRegex =~ name
  378. yield name
  379. return
  380. end
  381. yielded = false
  382. @resolvers.each {|r|
  383. r.each_address(name) {|address|
  384. yield address.to_s
  385. yielded = true
  386. }
  387. return if yielded
  388. }
  389. end
  390. #Looks up the first hostname of +address+
  391. def getname(address)
  392. each_name(address) {|name| return name}
  393. raise ResolvError.new("no name for #{address}")
  394. end
  395. #Looks up all hostnames of +address+
  396. def getnames(address)
  397. ret = []
  398. each_name(address) {|name| ret << name}
  399. return ret
  400. end
  401. #Iterates over all hostnames of +address+
  402. def each_name(address)
  403. yielded = false
  404. @resolvers.each {|r|
  405. r.each_name(address) {|name|
  406. yield name.to_s
  407. yielded = true
  408. }
  409. return if yielded
  410. }
  411. end
  412. require 'Dnsruby/DNS'
  413. require 'Dnsruby/Hosts'
  414. require 'Dnsruby/message'
  415. require 'Dnsruby/DNSSEC'
  416. require 'Dnsruby/update'
  417. require 'Dnsruby/zone_transfer'
  418. #Default Resolver to use for Dnsruby class methods
  419. DefaultResolver = self.new
  420. #Address RegExp to use for matching IP addresses
  421. AddressRegex = /(?:#{IPv4::Regex})|(?:#{IPv6::Regex})/
  422. end
  423. end