File Coverage

blib/lib/Net/DNS/QueryID.pm
Criterion Covered Total %
statement 22 23 95.6
branch 8 10 80.0
condition 6 9 66.6
subroutine 6 7 85.7
pod 3 3 100.0
total 45 52 86.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             package Net::DNS::QueryID;
3              
4 3     3   3437 use strict;
  3         4  
  3         125  
5             #use diagnostics;
6              
7 3         1732 use vars qw(
8             $VERSION
9             @ISA
10             @EXPORT_OK
11 3     3   15 );
  3         4  
12              
13             require Exporter;
14             @ISA = qw(Exporter);
15              
16              
17             $VERSION = do { my @r = (q$Revision: 0.02 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
18              
19             @EXPORT_OK = qw(
20             id_get
21             id_clr
22             id_busy
23             );
24              
25 0     0   0 sub DESTROY {};
26              
27             =head1 NAME
28              
29             Net::DNS::QueryID - random Query ID numbers
30              
31             =head1 SYNOPSIS
32              
33             Functions to manage a cache of random Query ID's for DNS packets.
34              
35             The purpose of this module is to provide and unpredictable source of 16 bit
36             DNS Query ID numbers to help defeat cache poisoning using DNS Spoofing or "Man in the Middle"
37             attacks as describe in the Wikipedia article and its references:
38              
39             http://en.wikipedia.org/wiki/DNS_cache_poisoning
40              
41             use Net::DNS::QueryID qw(
42             id_get
43             id_clr
44             id_busy
45             );
46              
47             $queryID = id_get();
48             $result = id_clr($queryID);
49             $result = id_busy($queryID);
50              
51             =cut
52              
53             my $idvec = '';
54             foreach(0..2047) { # set 65536 long vector string to zero
55             vec($idvec,$_,32) = 0x0;
56             }
57              
58             my $test = 0;
59              
60             =item * $queryID = id_get();
61              
62             input: none
63             returns: 16 bit integer from 1 - 65535
64             that is not currently in the cache.
65             false (0) if all 65535 ID's are in use
66              
67             =cut
68              
69             sub id_get() {
70 65550   66 65550 1 347142 my $try = $test || int(rand(65534)) + 1; # a number between 1 and 65535
71 65550         57674 my $first = $try;
72 65550         115490 while (vec($idvec,$try,1)) {
73 7537714         6690750 $try++;
74 7537714 100       11143881 $try = 1 if $try > 65535;
75 7537714 100       17237519 return 0 if $try == $first; # oops, sorry, all 65535 id's in use
76             }
77 65549         106594 vec($idvec,$try,1) = 0x1;
78 65549         120016 $try;
79             }
80              
81             =item * $result = id_clr($queryID);
82              
83             input: Query ID to clear
84             returns: true (the Query ID) on success
85             false if the Query ID is not in use
86             false if the Query ID is out of range
87             i.e. not 1 -1 65535
88              
89             =cut
90              
91             sub id_clr($) {
92 10 50 33 10 1 1790 return 0 if $_[0] < 1 || $_[0] > 65535;
93 10 50       23 return 0 unless vec($idvec,$_[0],1);
94 10         75 vec($idvec,$_[0],1) = 0x0;
95 10         26 return $_[0];
96             }
97              
98             =item * $result = id_busy($queryID);
99              
100             input: Query ID
101             returns: true if Query ID is in the cache
102             false if Query ID is not in the cache
103             false if Query ID is out of range
104             i.e. not 1 -165535
105              
106             =cut
107              
108             sub id_busy($) {
109 65551 100 100 65551 1 315318 return 0 if $_[0] < 1 or $_[0] > 65535;
110 65549         96060 vec($idvec,$_[0],1);
111             }
112              
113             sub _mode {
114 8     8   1489 $test = $_[0];
115 8         76 return $idvec;
116             }
117              
118             =head1 EXPORTS_OK
119              
120             id_get
121             id_clr
122             id_busy
123              
124             =head1 AUTHOR
125              
126             Michael Robinton
127              
128             =head1 COPYRIGHT 2012-2014
129              
130             Michael Robinton
131              
132             All rights reserved.
133              
134             This program is free software; you can redistribute it and/or modify
135             it under the terms of either:
136              
137             a) the GNU General Public License as published by the Free
138             Software Foundation; either version 2, or (at your option) any
139             later version, or
140              
141             b) the "Artistic License" which comes with this distribution.
142              
143             This program is distributed in the hope that it will be useful,
144             but WITHOUT ANY WARRANTY; without even the implied warranty of
145             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
146             the GNU General Public License or the Artistic License for more details.
147              
148             You should have received a copy of the Artistic License with this
149             distribution, in the file named "Artistic". If not, I'll be glad to provide
150             one.
151              
152             You should also have received a copy of the GNU General Public License
153             along with this program in the file named "Copying". If not, write to the
154              
155             Free Software Foundation, Inc.
156             59 Temple Place, Suite 330
157             Boston, MA 02111-1307, USA
158              
159             or visit their web page on the internet at:
160              
161             http://www.gnu.org/copyleft/gpl.html.
162              
163             =cut
164              
165             1;