blib/lib/Net/DNS/QueryID.pm

filter: Statementstmt 100% 33 / 33 Branchbran 83.3% 10 / 12 Conditioncond 66.6% 6 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 85.7% 6 / 7 Podpod 100% 3 / 3 totaltotal 85.7% 60 / 70 CC 10 SCAR 12.7
CC
10
Cov
91%
id_get
5.0
id_clr
4.8
id_busy
3.0
CRAP 3.6
↑ Summary
1 #!/usr/bin/perl
2 package Net::DNS::QueryID;
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use diagnostics;
6  
7 3 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 $VERSION
9 @ISA
10 @EXPORT_OK
11 3 );
12  
13 3 require Exporter;
14 3 @ISA = qw(Exporter);
15  
16  
17 3 $VERSION = do { my @r = (q$Revision: 0.02 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
18  
19 3 @EXPORT_OK = qw(
20 id_get
21 id_clr
22 id_busy
23 );
24  
25 0 ▶sub DESTROY {};
Subroutine DESTROY: not called CC=1 SCAR=0.0
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 3 my $idvec = '';
54 3 foreach(0..2047) { # set 65536 long vector string to zero
55 6144 vec($idvec,$_,32) = 0x0;
56 }
57  
58 3 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 65550 ▶sub id_get() {
Subroutine id_get: called CC=5 SCAR=16.1
Pod documented
70 65550 ▶ my $try = $test || int(rand(65534)) + 1; # a number between 1 and 65535
Condition66%2/3
$test || int(rand 65534) + 1
l!l&&r!l&&!r
8655420
MC/DC0%0/2
$test || int(rand 65534) + 1
$test int(rand 65534) + 1
Truth table66%2/3
A || B
A= $testB= int(rand 65534) + 1
ABresult
1X1
011
000
71 65550 my $first = $try;
72 65550 ▶ while (vec($idvec,$try,1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $idvec, $try, 1) 11495204 65549 11560753
73 11495204 $try++;
74 11495204 ▶ $try = 1 if $try > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $try > 65535 103 11495101 11495204
75 11495204 ▶ return 0 if $try == $first; # oops, sorry, all 65535 id's in use
Branch: 1 branch
BranchTrueFalseTotal
if $try == $first 1 11495203 11495204
76 }
77 65549 vec($idvec,$try,1) = 0x1;
78 65549 $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 10 ▶sub id_clr($) {
Subroutine id_clr: called CC=4 SCAR=15.6
Pod documented
92 10 ▶ return 0 if $_[0] < 1 || $_[0] > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 1 or $_[0] > 65535 0 10 10
Condition33%1/3
$_[0] < 1 or $_[0] > 65535
l!l&&r!l&&!r
0010
MC/DC0%0/2
$_[0] < 1 or $_[0] > 65535
$_[0] < 1 $_[0] > 65535
Truth table33%1/3
A or B
A= $_[0] < 1B= $_[0] > 65535
ABresult
1X1
011
000
93 10 ▶ return 0 unless vec($idvec,$_[0],1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $idvec, $_[0], 1 0 10 10
94 10 vec($idvec,$_[0],1) = 0x0;
95 10 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 65551 ▶sub id_busy($) {
Subroutine id_busy: called CC=3 SCAR=11.0
Pod documented
109 65551 ▶ return 0 if $_[0] < 1 or $_[0] > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < 1 or $_[0] > 65535 2 65549 65551
Condition100%3/3
$_[0] < 1 or $_[0] > 65535
l!l&&r!l&&!r
1165549
MC/DC100%2/2
$_[0] < 1 or $_[0] > 65535
$_[0] < 1 $_[0] > 65535
Truth table100%3/3
A or B
A= $_[0] < 1B= $_[0] > 65535
ABresult
1X1
011
000
110 65549 vec($idvec,$_[0],1);
111 }
112  
113 8 ▶sub _mode {
Subroutine _mode: called CC=1 SCAR=0.0
114 8 $test = $_[0];
115 8 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 <michael@bizsystems.com>
127  
128 =head1 COPYRIGHT 2012-2014
129  
130 Michael Robinton <michael@bizsystems.com>
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 3 1;
↑ Summary