blib/lib/DNS/TinyDNS/dnscache.pm

filter: Statementstmt 14.1% 12 / 85 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 33.3% 3 / 9 Podpod 85.7% 6 / 7 totaltotal 9.7% 21 / 216 CC 42 SCAR 49.0
CC
42
Cov
7%
del_server
306.0
add_server
72.0
list_servers
72.0
CRAP 134.7
« blib/lib/DNS/TinyDNS/dnsserver.pm ↑ Summary blib/lib/DNS/TinyDNS.pm »
1 ###
2 # by Anarion
3 # anarion@7a69ezine.org
4 package DNS::TinyDNS::dnscache;
5  
6 1 our @ISA = qw(DNS::TinyDNS);
7 1 our $VERSION = '0.22';
8  
9 =head1 NAME
10  
11 DNS::TinyDNS::dnscache - Perl extension for manipulating dnscache from djbdns
12  
13 =head1 SYNOPSIS
14  
15 use DNS::TinyDNS;
16  
17 # First create the object
18 my $dnscache = DNS::TinyDNS->new(type => 'dnscache',
19 dir => '/service/dnscachex');
20  
21 # Manage the allowed ips to use this cache
22 my @ips=$dnscache->list_ips;
23 $dnscache->add_ip('10.0.0.1');
24 $dnscache->del_ip('10.0.0');
25  
26 # Manage root servers
27 my @root_servers=$dnscache->list_servers;
28 $dnscache->add_server('10.0.0.1');
29 $dnscache->del_server('10.0.0.1');
30  
31 # Manage the enviroment
32 $dnscache->set_env( cachesize => 100000,
33 ip => '10.0.0.1');
34 my ($cache,$ip) = $dnscache->get_env( 'cachesize', 'ip' );
35  
36 =head1 DESCRIPTION
37  
38 This module will allow you to manipulate djbdns dnscache files.
39  
40 =head1 FUNCTIONS
41  
42 =over 4
43  
44 =head2 list_ips
45  
46 =back
47  
48 Returns a list/reference of all the ips/nets allowed to use this cache server
49  
50 # Returns a list
51 my @ips = $dnscache->list_ips;
52  
53 # Returns an array ref
54 my $ips = $dnscache->list_ips;
55  
56 =over 4
57  
58 =head2 add_ip
59  
60 =back
61  
62 Adds an ips/nets to use this cache server
63  
64 $dnscache->add_ip('10.0.0');
65  
66 This let all 10.0.0.0/24 to use this dnsserver.
67  
68 =over 4
69  
70 =head2 del_ip
71  
72 =back
73  
74 Remove an ips/nets of the list of allowed ips
75  
76 $dnscache->del_ip('10.0.0');
77  
78 This deletes C<All entries> of 10.0.0.0/24 allowed to use this dnscache.
79  
80 =over 4
81  
82 =head2 lists_servers
83  
84 =back
85  
86 Returns a list/reference of the root servers
87  
88 # Returns a list
89 my @root_servers = $dnscache->list_servers;
90  
91 # Returns an array ref
92 my $root_servers = $dnscache->list_servers;
93  
94 =over 4
95  
96 =head2 add_server
97  
98 =back
99  
100 Add a root server
101  
102 $dnscache->add_server('10.0.0.1')
103 or warn "Cant add server";
104  
105 =head2 del_server
106  
107  
108 Deletes a root server
109  
110 $dnscache->del_server('10.0.0.1')
111 or warn "Cant del server";
112  
113 =over 4
114  
115 =head2 get_env set_env
116  
117 =back
118  
119 You can set/get this vars:
120  
121 IP
122 IPSEND
123 CACHESIZE
124 DATALIMIT
125 ROOT
126  
127 For further information about every var, consult djbdns cache documentation at
128 C<http://cr.yp.to/>
129  
130 =head1 AUTHOR
131  
132 Anarion: anarion@7a69ezine.org
133  
134 This library is free software; you can redistribute it and/or modify
135 it under the same terms as Perl itself.
136  
137 =head1 SEE ALSO
138  
139 L<DNS::TinyDNS>.
140 L<DNS::TinyDNS::dnsserver>.
141  
142 =cut
143  
144 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 1 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
146  
147 sub new
148 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
149 1 my ($clase,$dir) = @_;
150 1 my $self = { dir => $dir,
151 t_env => { IP => '',
152 IPSEND => '',
153 CACHESIZE => '',
154 DATALIMIT => '',
155 ROOT => ''
156 },
157 svc => '/usr/local/bin/svc'
158 };
159 1 return bless $self,$clase;
160 }
161  
162  
163 sub add_ip
164 0 ▶{
Subroutine add_ip: not called CC=5 SCAR=34.0
Pod documented
165 0 my ($self,$ip) = @_;
166 0 my $dir = $self->{dir} . "/root/ip";
167 0 local *FILE;
168  
169 0 ▶ unless($self->{dir} and -d $dir)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dir"} and -d $dir) 0 0 0
Condition0%0/3
$self->{"dir"} and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dir"} and -d $dir
$self->{"dir"} -d $dir
Truth table0%0/3
A and B
A= $self->{"dir"}B= -d $dir
ABresult
0X0
100
111
170 {
171 0 carp "ERROR: dnscache directory not set";
172 0 return 0;
173 }
174  
175 0 ▶ unless(defined $ip)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip) 0 0 0
176 {
177 0 carp "ERROR: You must supply an ip ($ip)";
178 0 return 0;
179 }
180  
181 0 ▶ open(FILE,">$dir/$ip")
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$dir/$ip" or carp("ERROR: Cant create $dir/$ip") 0 0 0
182 or carp "ERROR: Cant create $dir/$ip";
183 0 close(FILE);
184 }
185  
186 sub del_ip
187 0 ▶{
Subroutine del_ip: not called CC=4 SCAR=30.0
Pod documented
188 0 my ($self,$ip) = @_;
189 0 my $dir = $self->{dir} . "/root/ip";
190  
191 0 ▶ unless($self->{dir} and -d $dir)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dir"} and -d $dir) 0 0 0
Condition0%0/3
$self->{"dir"} and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dir"} and -d $dir
$self->{"dir"} -d $dir
Truth table0%0/3
A and B
A= $self->{"dir"}B= -d $dir
ABresult
0X0
100
111
192 {
193 0 carp "ERROR: dnscache directory not set";
194 0 return 0;
195 }
196  
197 0 ▶ unlink("$dir/$ip")
Branch: 1 branch
BranchTrueFalseTotal
unlink "$dir/$ip" or carp("Warning: That ip wasn't in the db") 0 0 0
198 or carp "Warning: That ip wasn't in the db";#'
199 }
200  
201 sub list_ips
202 0 ▶{
Subroutine list_ips: not called CC=5 SCAR=34.0
Pod documented
203 0 my $self = shift;
204 0 my $dir = $self->{dir} . "/root/ip";
205 0 my @ips;
206 0 local *FILE;
207  
208 0 ▶ unless($self->{dir} and -d $dir)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dir"} and -d $dir) 0 0 0
Condition0%0/3
$self->{"dir"} and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dir"} and -d $dir
$self->{"dir"} -d $dir
Truth table0%0/3
A and B
A= $self->{"dir"}B= -d $dir
ABresult
0X0
100
111
209 {
210 0 carp "ERROR: dnscache directory not set";
211 0 return 0;
212 }
213  
214 0 ▶ opendir(FILE,$dir)
Branch: 1 branch
BranchTrueFalseTotal
opendir FILE, $dir or carp("ERROR: Cant read $dir") 0 0 0
215 or carp "ERROR: Cant read $dir";
216 0 @ips = grep { index($_,".") != 0 } readdir(FILE);
217 0 closedir(FILE);
218 0 ▶ return wantarray ? @ips : \@ips;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
219 }
220  
221 sub add_server
222 0 ▶{
Subroutine add_server: not called CC=8 SCAR=42.8
Pod documented
223 0 my ($self,$ip) = @_;
224 0 my $file = $self->{dir} . '/root/servers/@';
225 0 my @array;
226 0 local *FILE;
227  
228 0 ▶ unless($self->{dir} and -f $file)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dir"} and -f $file) 0 0 0
Condition0%0/3
$self->{"dir"} and -f $file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dir"} and -f $file
$self->{"dir"} -f $file
Truth table0%0/3
A and B
A= $self->{"dir"}B= -f $file
ABresult
0X0
100
111
229 {
230 0 carp "ERROR: dnscache directory not set";
231 0 return 0;
232 }
233  
234 0 ▶ open(FILE, ">>$file")
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">>$file" or carp("ERROR: Cant write to $file") and return 0 0 0
Condition0%0/5
open(FILE, ">>$file") || carp("ERROR: Cant write to $file") && (return)
l!l&&r!l&&!r
000
carp("ERROR: Cant write to $file") and return
!ll
00
MC/DC0%0/2
open(FILE, ">>$file") || carp("ERROR: Cant write to $file") && (return)
open(FILE, ">>$file") carp("ERROR: Cant write to $file")
Truth table0%0/3
A || B
A= open(FILE, ">>$file")B= carp("ERROR: Cant write to $file")
ABresult
1X1
011
000
235 or carp "ERROR: Cant write to $file" and return;
236 0 ▶ flock(FILE,LOCK_EX)
Branch: 1 branch
BranchTrueFalseTotal
flock FILE, 2 or carp("ERROR: Cant lock $file") 0 0 0
237 or carp "ERROR: Cant lock $file";
238 0 ▶ seek(FILE,0,2)
Branch: 1 branch
BranchTrueFalseTotal
seek FILE, 0, 2 or carp("ERROR: Cant seek $file") 0 0 0
239 or carp "ERROR: Cant seek $file";
240 0 print FILE "$ip\n";
241 0 ▶ close(FILE)
Branch: 1 branch
BranchTrueFalseTotal
close FILE or carp("ERROR: Cant close $file") 0 0 0
242 or carp "ERROR: Cant close $file";
243 }
244  
245 sub del_server
246 0 ▶{
Subroutine del_server: not called CC=17 SCAR=57.2
Pod documented
247 0 my ($self,$ip) = @_;
248 0 my $file = $self->{dir} . '/root/servers/@';
249 0 my @array;
250 0 local (*FILENEW,*FILEOLD);
251  
252 0 ▶ unless($self->{dir} and -f $file)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dir"} and -f $file) 0 0 0
Condition0%0/3
$self->{"dir"} and -f $file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dir"} and -f $file
$self->{"dir"} -f $file
Truth table0%0/3
A and B
A= $self->{"dir"}B= -f $file
ABresult
0X0
100
111
253 {
254 0 carp "ERROR: $self->{dir} isn't a dnscache directory";#'
255 0 return 0;
256 }
257  
258 0 ▶ open(FILENEW, ">$self->{dir}/root/servers/new")
Branch: 1 branch
BranchTrueFalseTotal
open FILENEW, qq[>$self->{"dir"}/root/servers/new] or carp(qq[ERROR: Cant write to $self->{"dir"}/root/servers/new]) and return 0 0 0
Condition0%0/5
open(FILENEW, ">$self->{'dir'}/root/servers/new") || carp("ERROR: Cant write to $self->{'dir'}/root/servers/new") && (return)
l!l&&r!l&&!r
000
carp(qq[ERROR: Cant write to $self->{"dir"}/root/servers/new]) and return
!ll
00
MC/DC0%0/2
open(FILENEW, ">$self->{'dir'}/root/servers/new") || carp("ERROR: Cant write to $self->{'dir'}/root/servers/new") && (return)
open(FILENEW, ">$self->{'dir'}/root/servers/new") carp(qq[ERROR: Cant write to $self->{"dir"}/root/servers/new])
Truth table0%0/3
A || B
A= open(FILENEW, ">$self->{'dir'}/root/servers/new")B= carp(qq[ERROR: Cant write to $self->{"dir"}/root/servers/new])
ABresult
1X1
011
000
259 or carp "ERROR: Cant write to $self->{dir}/root/servers/new" and return;
260 0 ▶ flock(FILENEW,LOCK_EX)
Branch: 1 branch
BranchTrueFalseTotal
flock FILENEW, 2 or carp(qq[ERROR: Cant lock $self->{"dir"}/root/servers/new]) 0 0 0
261 or carp "ERROR: Cant lock $self->{dir}/root/servers/new";
262 0 ▶ open(FILEOLD,$file)
Branch: 1 branch
BranchTrueFalseTotal
open FILEOLD, $file or carp("ERROR: Cant read from $file") and return 0 0 0
Condition0%0/5
open(FILEOLD, $file) || carp("ERROR: Cant read from $file") && (return)
l!l&&r!l&&!r
000
carp("ERROR: Cant read from $file") and return
!ll
00
MC/DC0%0/2
open(FILEOLD, $file) || carp("ERROR: Cant read from $file") && (return)
open(FILEOLD, $file) carp("ERROR: Cant read from $file")
Truth table0%0/3
A || B
A= open(FILEOLD, $file)B= carp("ERROR: Cant read from $file")
ABresult
1X1
011
000
263 or carp "ERROR: Cant read from $file" and return;
264 0 ▶ flock(FILEOLD,LOCK_EX)
Branch: 1 branch
BranchTrueFalseTotal
flock FILEOLD, 2 or carp("ERROR: Cant lock $file") 0 0 0
265 or carp "ERROR: Cant lock $file";
266 0 ▶ seek(FILEOLD,0,0)
Branch: 1 branch
BranchTrueFalseTotal
seek FILEOLD, 0, 0 or carp("ERROR: Cant seek $file") 0 0 0
267 or carp "ERROR: Cant seek $file";
268 0 ▶ seek(FILENEW,0,0)
Branch: 1 branch
BranchTrueFalseTotal
seek FILENEW, 0, 0 or carp(qq[ERROR: Cant seek $self->{"dir"}/root/servers/new]) 0 0 0
269 or carp "ERROR: Cant seek $self->{dir}/root/servers/new";
270 0 ▶ while(my $line = <FILEOLD>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline FILEOLD)) 0 0 0
271 {
272 0 ▶ syswrite(FILENEW,$line) if index($line,$ip) == -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($line, $ip) == -1 0 0 0
273 }
274 0 ▶ close(FILENEW)
Branch: 1 branch
BranchTrueFalseTotal
close FILENEW or carp(qq[ERROR: Cant close $self->{"dir"}/root/servers/new]) 0 0 0
275 or carp "ERROR: Cant close $self->{dir}/root/servers/new";
276 0 ▶ close(FILEOLD)
Branch: 1 branch
BranchTrueFalseTotal
close FILEOLD or carp(qq[ERROR: Cant close $self->{"dir"}/root/servers/new]) 0 0 0
277 or carp "ERROR: Cant close $self->{dir}/root/servers/new";
278 0 ▶ unlink($file)
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or carp(qq[ERROR: Cant unlink $self->{"dir"}/root/servers/new]) 0 0 0
279 or carp "ERROR: Cant unlink $self->{dir}/root/servers/new";
280 0 ▶ rename($self->{dir} . '/root/servers/new',$file)
Branch: 1 branch
BranchTrueFalseTotal
rename $self->{"dir"} . '/root/servers/new', $file or carp("ERROR: Cant rename new to $file") 0 0 0
281 or carp "ERROR: Cant rename new to $file";
282 }
283  
284 sub list_servers
285 0 ▶{
Subroutine list_servers: not called CC=8 SCAR=42.8
Pod undocumented
286 0 my $self = shift;
287 0 my $file = $self->{dir} . '/root/servers/@';
288 0 my @root_servers;
289 0 local *FILE;
290  
291 0 ▶ unless($self->{dir} and -f $file)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dir"} and -f $file) 0 0 0
Condition0%0/3
$self->{"dir"} and -f $file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dir"} and -f $file
$self->{"dir"} -f $file
Truth table0%0/3
A and B
A= $self->{"dir"}B= -f $file
ABresult
0X0
100
111
292 {
293 0 carp "ERROR: dnscache directory not set ($file)";
294 0 return 0;
295 }
296  
297 0 ▶ open(FILE,$file)
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or carp("ERROR: Cant read from $file") 0 0 0
298 or carp "ERROR: Cant read from $file";
299 0 ▶ flock(FILE,LOCK_EX)
Branch: 1 branch
BranchTrueFalseTotal
flock FILE, 2 or carp("Cant lock $file") 0 0 0
300 or carp "Cant lock $file";
301 0 ▶ seek(FILE,0,0)
Branch: 1 branch
BranchTrueFalseTotal
seek FILE, 0, 0 or carp("ERROR: Cant seek $file") 0 0 0
302 or carp "ERROR: Cant seek $file";
303 0 chomp(@root_servers=<FILE>);
304 0 ▶ close(FILE)
Branch: 1 branch
BranchTrueFalseTotal
close FILE or carp("ERROR: Cant close $file") 0 0 0
305 or carp "ERROR: Cant close $file";
306 0 ▶ return wantarray ? @root_servers : \@root_servers;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
307 }
308  
309 1 1;
« blib/lib/DNS/TinyDNS/dnsserver.pm ↑ Summary blib/lib/DNS/TinyDNS.pm »