File: DNSHash.pm

package info (click to toggle)
libnet-dns-perl 0.19-0.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 544 kB
  • ctags: 323
  • sloc: perl: 5,191; makefile: 54
file content (374 lines) | stat: -rw-r--r-- 8,937 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package DNSHash::MX;
#
# This module is Copyright (c) 1997 Dave Hayes. All rights reserved.
#
# This program 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 1, or (at your option)
# any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

=head1 NAME

DNSHash::MX - Put DNS MX queries in a hash lookup format

=head1 SYNOPSIS

require DNSHash;

tie %DNS, "DNSHash::MX", <preload>, <cachedb>, <usettl>

$mxhostname = $DNS{"host.do.main"};

=head1 DESCRIPTION

This implements DNS MX lookups as a tied hash. It takes the first
answer at the lowest priority (like sendmail would to). 

If the file <preload> is defined, the hash is preloaded with the
hostname/mxhostname pairs found in this file. Format is

    mxhostname   hostname

Assignments to this hash are added to the preload lookup. 
You can delete preload lookups by delete()ing the entry.

Lookups are cached once done if <cachedb> is defined.

Cache entries are held for <usettl> seconds unless <usettl> 
undefined, in which case the TTL of the record is used
in each case.

You can clear the cache by removing <cachedb>. If you want
to clear a particular lookup in the cache, use delete().

each() on these hashes only walks the preload.

=cut

BEGIN { @ISA = qw(DNSHash); }

sub doLookup {
    my ($self,$res,$key) = @_;

    my $packet = $res->search("$key","MX");
    if (!defined($packet)) {
	warn "DNS query failed: ", $res->errorstring, "\n";
	return (undef,undef);
    }

    my ($value,$ttl,$minpref); 
    foreach $answer ($packet->answer) {
	my $type = $answer->type;
	if ($type eq "MX") {
	    my $pref = $answer->preference;
	    if (!defined($minpref) || $minpref > $pref) {
		$value = $answer->exchange;
		$ttl = $answer->ttl;
		$minpref = $pref;
	    }
	}
    }
    ($value,$ttl);
}

package DNSHash;

=head1 NAME

DNSHash - Put DNS hostname/ip queries in a hash lookup format

=head1 SYNOPSIS

require DNSHash;

tie %DNS, "DNSHash", <preload>, <cachedb>, <usettl>

$hostname = $DNS{"a.b.c.d"};
$ip = $DNS{"host.do.main"};

$DNS{"host.do.main"} = $realip;

=head1 DESCRIPTION

This implements DNS A and PTR record lookup as a tied hash. 

If the file <preload> is defined, the hash is preloaded with the
hostname/ip pairs found in the file (format is like /etc/hosts).

Assignments to this hash are added to the preload lookup. 
You can delete preload lookups by delete()ing the entry.

Lookups are cached once done if <cachedb> is defined.

Cache entries are held for <usettl> seconds unless <usettl> 
undefined, in which case the TTL of the record is used
in each case.

You can clear the cache by removing <cachedb>. If you want
to clear a particular lookup in the cache, use delete().

each() on these hashes only walks the preload.

=cut
#################
#
#!$Id: DNSHash.pm,v 1.3 1997/08/15 04:26:10 dave Exp dave $
#!$Log: DNSHash.pm,v $
#!Revision 1.3  1997/08/15 04:26:10  dave
#!Fixed bug in Cache loading if cache was not present or you couldn't create the cache file
#!
#!Revision 1.2  1997/07/18 00:49:50  dave
#!Added DNSHash::MX functionality.
#!
#!Revision 1.1  1997/06/24 03:48:23  dave
#!Initial revision
#!
#
################
#
use strict;

use vars       qw($VERSION @ISA $MyQuery);

require Tie::Hash;

BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File SDBM_File); }
require AnyDBM_File;

use Fcntl qw(O_WRONLY O_RDWR O_CREAT O_EXCL);

require Net::DNS;

use Carp;

BEGIN {
    $VERSION = do { my @r = (q$Revision: 1.3 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; #
    @ISA         = qw(Tie::Hash);
}

# Constructor
sub TIEHASH {
    my $type = shift;
    my ($preload,$cache,$usettl) = @_;
    my ($cache_hash,$dnshash); 

    if (defined($cache)) {
	$cache_hash = AnyDBM_File->new($cache, O_CREAT|O_RDWR, 0600);
	if (!defined($cache_hash)) {
	    carp "Cannot open DNS cache file '$cache': $!\nDefaulting to in memory cache.\n";
	    $cache_hash = AnyDBM_File->new(undef, O_CREAT|O_RDWR, 0600);
	    croak "Cannot even make an in memory cache, '$!',  dying...\n" if (!defined($cache_hash));
	}
    }

    $dnshash = {
	'preload_file' => $preload,
	'preload' => undef,
	'cache' => $cache,
	'cache_hash' => $cache_hash,
	'usettl' => $usettl,
	'resolver' => Net::DNS::Resolver->new(),
    };

    load_preload($dnshash);
    bless $dnshash, $type;
}

sub load_preload {
    my ($hash) = @_;

    my $preload = $hash->{"preload_file"};
    if (-e "$preload") {
	if (open(PRE,"<$preload")) {
	    my $preload_data = {};
	    while(<PRE>) {
		next if (/^\s*$/ || /^\#/);
		my ($ip,$hostname) = split(/\s+/);
		$preload_data->{"$hostname"} = $ip;
		$preload_data->{"$ip"} = $hostname;
	    }
	    close(PRE);
	    $hash->{"preload"} = $preload_data;
	} else {
	    carp "Cannot open DNS preload file '$preload': $!\n";
	}
    }
    1;
}

sub cache_check {
    my ($hash) = @_;
    my $cache = $hash->{"cache"};
    my $chash = $hash->{"cache_hash"};

    if (! -e "$cache") {
	# Whoops, clear the cache
	$chash->DESTROY;
	$chash = AnyDBM_File->new($cache, O_CREAT|O_RDWR, 0600);
	carp "Cannot open DNS cache file '$cache': $!\n";
	$hash->{"cache_hash"} = $chash;
    } 
    return $chash;
}

# FETCH - Get a single unit of data via key
sub FETCH {
    my ($self,$key) = @_;

#    warn "--\tFETCH\n";
    # Try the preloaded first
    my $preload = $self->{"preload"};
    if (defined($preload)) {
#	warn "--\t Preload\n";
	my $value = $preload->{"$key"};
	return $value if (defined($value));
    }
    
    # Ok, now do a cache lookup
    my $chash = cache_check($self);
    my $lookup = $chash->FETCH($key);
    if (defined($lookup)) {
#	warn "--\t Ow!\n";
	# The Cache Hit. ;-)
	my ($expire,$value) = split(/:/,$lookup);
	return $value if (time < $expire);
    }
    
    # Well, gotta do a DNS lookup.
    # So i can subclass effectively, this is done in another subroutine
#    warn "--\t Lookup!\n";
    my $res = $self->{"resolver"};
    my $usettl = $self->{"usettl"};
    my ($value,$ttl) = $self->doLookup($res,$key);
    $ttl = $usettl if (defined($usettl));

    # Cache value and go
    if (defined($value)) {
	my $expire = $ttl + time;
	$chash->STORE("$key","$expire:$value");
#	warn "--\tStored: $key => $ttl:$value\n";
    }
    return $value;
}

sub doLookup {
    my ($self,$res,$key) = @_;

    my $packet = $res->search("$key");
    if (!defined($packet)) {
	warn "DNS query failed: ", $res->errorstring, "\n";
	return (undef,undef);
    }

    my ($value,$ttl); 
    foreach my $answer ($packet->answer) {
	my $type = $answer->type;
	$ttl = $answer->ttl;
	if ($type eq "A") {
	    $value = $answer->address;
	} elsif ($type eq "PTR") {
	    $value = $answer->ptrdname;
	}
	last if (defined($value));
    }
    ($value,$ttl);
}


# STORE - Store data, in this case in the preload
sub STORE {
    my ($self,$key,$value) = @_;

#    warn "--\tSTORE\n";    
    my $preload = $self->{"preload"};
    if (!defined($preload)) {
	$self->{"preload"} = {};
	$preload = $self->{"preload"};
    }
    $preload->{"$key"} = $value;
    1;
}

# DELETE - Delete data, first from preload, then from cache
sub DELETE {
    my ($self,$key) = @_;

#    warn "--\tDELETE\n";
    my $preload = $self->{"preload"};
    if (defined($preload)) {
#	warn "--\tDeleting preload\n";
	delete($preload->{"$key"});
	return undef;
    }
#    warn "--\tNo preload\n";
    my $chash = cache_check($self);
    $chash->DELETE("$key");
    return undef;
}

# CLEAR - Clears the cache and resets the preload.
sub CLEAR {
    my $self = shift;

#    warn "--\tCLEAR\n";
    my $preload = $self->{"preload"};
    if (defined($preload)) {
	$self->{"preload"} = undef;
	load_preload($self);
    }
    my $chash = cache_check($self);
    $chash->CLEAR();
    1;
}

# EXISTS - Does this key exist?
sub EXISTS {
    my ($self,$key) = @_;

#    warn "--\tEXISTS\n";
    # Try the preloaded first
    my $preload = $self->{"preload"};
    return exists($preload->{"$key"}) if (defined($preload));
    
    # Ok, now do a cache lookup
    my $chash = cache_check($self);
    return $chash->EXISTS($key);
}

# FIRSTKEY - First in iteration.
sub FIRSTKEY {
    my ($self) = @_;

#   warn "--\tFIRSTKEY\n";

    my $preload = $self->{"preload"};

    # Reset the scan
    my $trash = keys %$preload; 

    return each (%{$preload});
}

# NEXTKEY - Next in iteration
sub NEXTKEY {
    my ($self,$key) = @_;
    
#    warn "--\tNEXTKEY\n";
    my $preload = $self->{"preload"};
    return each(%{$preload});
}    

# End of Package
1;