File: cachemanager.rb

package info (click to toggle)
zonecheck 2.0.4-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,416 kB
  • ctags: 1,177
  • sloc: ruby: 9,582; xml: 731; sh: 404; makefile: 71
file content (277 lines) | stat: -rw-r--r-- 6,607 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# $Id: cachemanager.rb,v 1.30 2003/11/27 14:19:29 sdalu Exp $

# 
# CONTACT     : zonecheck@nic.fr
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
#
# CREATED     : 2002/08/02 13:58:17
# REVISION    : $Revision: 1.30 $ 
# DATE        : $Date: 2003/11/27 14:19:29 $
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
# LICENSE     : GPL v2 (or MIT/X11-like after agreement)
# COPYRIGHT   : AFNIC (c) 2003
#
# This file is part of ZoneCheck.
#
# ZoneCheck is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# ZoneCheck is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZoneCheck; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

#####
#
# TODO:
#   - add a close/destroy function to destroy the cachemanager and free
#     the dns resources
#

require 'sync'
require 'cache'

##
## The CacheManager
##
class CacheManager
    ##
    ## Proxy an NResolv::DNS::Resource class
    ##
    ## This will allow to add access to extra fields that are normally
    ## provided only in the DNS message header:
    ##  - ttl    : time to live
    ##  - aa     : authoritative answer
    ##  - ra     : recursivity available
    ##  - r_name : resource name
    ##
    class ProxyResource
	attr_reader :ttl, :aa, :ra, :r_name

	#
	# Initialize proxy 
	#
	def initialize(resource, ttl, name, msg)
	    @resource = resource
	    @ttl      = ttl
	    @r_name   = name
	    @aa       = msg.aa
	    @ra       = msg.ra
	end
	
	#
	# Save the class method for later use
	# 
	alias _class class

	#
	# Equality should work with proxy object or real object
	#
	def eql?(other)
	    return false unless self.class == other.class
	    other = other.instance_eval('@resource') if respond_to?(:_class)
	    @resource == other
	end
	alias == eql?

	#
	# Redefine basic methods (hash, class, ...) to point to the
	# real object
	#
	def hash        ; @resource.hash        ; end
	def to_s        ; @resource.to_s        ; end
	def class       ; @resource.class       ; end
	def kind_of?(k) ; @resource.kind_of?(k) ; end
	alias instance_of? kind_of?
	alias is_a?        kind_of?

	#
	# Direct all unknown methods to the real object
	#
	def method_missing(method, *args)
	    @resource.method(method).call(*args)
	end
    end




    attr_reader :all_caches, :all_caches_m, :root

    def clear
	@cache.clear
    end


    private
    def initialize(root, dns, client)
	# Root node propagation
	@root		= root.nil? ? self      : root
	@all_caches	= root.nil? ? {}        : root.all_caches
	@all_caches_m	= root.nil? ? Sync::new : root.all_caches_m

	# DNS / client type
	@dns		= dns
	@client		= client

	# Cached items
	@cache = Cache::new
	@cache.create(:address, :soa, :any, :ns, :mx, :cname, :a, :aaaa, :ptr, :rec)
    end
    
    def get_resources(name, resource, rec=true, exception=false)
	res = []
	@dns.each_resource(name, resource, rec, exception) {|args|
	    res << ProxyResource::new(*args)
	}
	res
    end

    def get_resource(name, resource, rec=true, exception=false)
	@dns.each_resource(name, resource, rec, exception) {|args|
	    return ProxyResource::new(*args)
	}
	nil
    end


    public
    def [](ip, client=@client)
	@all_caches_m.synchronize {
	    # Is the root asked?
	    return @root if ip.nil?

	    # Sanity check
	    case ip
	    when Address
	    else raise 'Argument should be an Address'
	    end
 
	    # Retrieve/Create the cachemanager for the address
	    ip = ip.to_s
	    config = NResolv::DNS::Config::new([ ip ], [])
	    key    = [ip, client]
	    if (ic = @all_caches[key]).nil?
		dns    = client::new(config)
		ic     = CacheManager::new(@root, dns, client)
		@all_caches[key] = ic
	    end
	    ic
	}
    end

    # Create the root information cache
    def self.create(dns, client=NResolv::DNS::Client::STD)
	CacheManager::new(nil, dns, client)
    end



    #-- Shortcuts ----------------------------------------------------
    def addresses(host, order=Address::OrderDefault)
	host = NResolv::to_addrname(host)
	case host
	when Address
	    [ host ]
	when NResolv::DNS::Name
	    @cache.use(:address, host) {
		begin
		    @dns.getaddresses(host, order)
		rescue NResolv::DNS::NoEntryError
		    []
		end
	    }
	else
	    raise ArgumentError, 'Expecting Address or DNS Name'
	end
    end

    # ANY records
    def any(domainname, resource=nil, force=nil)
	res = @cache.use(:any, domainname, force) {
	    get_resources(domainname, NResolv::DNS::Resource::IN::ANY)
	}
	if resource.nil?
	    return res
	else
	    nres = [ ]
	    res.each { |r| nres << r if r.class == resource }
	    return nres
	end
    end

    # SOA record
    def soa(domainname, force=nil)
	@cache.use(:soa, domainname, force) {
	    get_resource(domainname,  NResolv::DNS::Resource::IN::SOA)
	}
    end

    # NS records
    def ns(domainname, force=nil)
	@cache.use(:ns, domainname, force) {
	    get_resources(domainname, NResolv::DNS::Resource::IN::NS)
	}
    end
    
    # MX record
    def mx(domainname, force=nil)
	@cache.use(:mx, domainname, force) {
	    get_resources(domainname, NResolv::DNS::Resource::IN::MX)
	}
    end
    
    # A record
    def a(name, force=nil)
	@cache.use(:a, name, force) {
	    get_resources(name,        NResolv::DNS::Resource::IN::A)
	}
    end
    
    # AAAA record
    def aaaa(name, force=nil)
	@cache.use(:aaaa, name, force) {
	    get_resources(name,        NResolv::DNS::Resource::IN::AAAA)
	}
    end
    
    # CNAME record
    def cname(name, force=nil)
	@cache.use(:cname, name, force) {
	    get_resource(name,        NResolv::DNS::Resource::IN::CNAME)
	}
    end

    # PTR records
    def ptr(name, force=nil)
	@cache.use(:ptr, name, force) {
	    get_resources(name,       NResolv::DNS::Resource::IN::PTR)
	}
    end	

    # TXT record
    def txt(name, force=nil)
	@cache.use(:ptr, name, force) {
	    get_resources(name,       NResolv::DNS::Resource::IN::TXT)
	}
    end
    
    #-- Shortcuts ----------------------------------------------------
    def rec(domainname, force=nil)
	@cache.use(:rec, domainname, force) {
	    soa = soa(domainname, force)
	    raise NResolv::NResolvError, 'Domain doesn\'t exists' if soa.nil?
	    soa.ra
	}
    end
end