blib/lib/DNS/TinyDNS.pm

filter: Statementstmt 26.2% 21 / 80 Branchbran 10.0% 4 / 40 Conditioncond 17.6% 3 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 6 / 12 Podpod 100% 8 / 8 totaltotal 25.4% 42 / 165 CC 27 SCAR 41.0
CC
27
Cov
20%
get_env
110.0
set_env
110.0
svc
12.0
CRAP 60.4
« blib/lib/DNS/TinyDNS/dnscache.pm ↑ Summary blib/lib/DNS/example.pl »
1 ###
2 # by Anarion
3 # anarion@7a69ezine.org
4 package DNS::TinyDNS;
5  
6 1 our $VERSION = '0.22';
7  
8 =head1 NAME
9  
10 DNS::TinyDNS - Perl extension for manipulating djbdns
11  
12 =head1 SYNOPSIS
13  
14 use DNS::TinyDNS;
15  
16 # Create a dnsserver or a dnscache
17 my $cache = DNS::TinyDNS->new(type => 'dnscache');
18 my $server = DNS::TinyDNS->new(type => 'dnsserver');
19  
20  
21 # Set the config directory
22 $server->dir('/service/tinydns');
23  
24 # start, stop and restart
25 $cache->stop;
26 $cache->start;
27 $server->restart;
28  
29 =head1 DESCRIPTION
30  
31 This module will allow you to manipulate djbdns files. You can
32 configure the options of dnscache, list, add, remove, or modify
33 all the entrys of your dns server too.
34  
35 Please check the documentation of L<DNS::TinyDNS::dnscache> or
36 L<DNS::TinyDNS::dnsserver> to see the details of each one.
37  
38  
39 =head1 FUNCTIONS
40  
41 =over 4
42  
43 =head2 new
44  
45 =back
46  
47 When you create a new object you have to specify if it's a dns server
48 or a dnscache.
49  
50 # Create a dnsserver
51 my $server=DNS::TinyDNS->new(type => 'dnsserver');
52  
53 # Create a dnscache
54 my $cache = DNS::TinyDNS->new(type => 'dnscache');
55  
56 You can specify the directory at this time or do it later.
57  
58 my $server = DNS::TinyDNS->new( type => 'dnsserver',
59 dir => '/service/tinydns');
60  
61 =over 4
62  
63 =head2 dir
64  
65 =back
66  
67 If you pass a parameter, it sets the directory, otherwise, it just
68 return the value of the directory.
69  
70 $server->dir('/etc/tinydns')
71 or warn "Cant change dir to /etc/tinydns";
72  
73 my $dir = $server->dir;
74  
75 =over 4
76  
77 =head2 get_env
78  
79 =back
80  
81 This method depends on the type of the object.
82  
83 =over 4
84  
85 =head2 set_env
86  
87 =back
88  
89 This method depends on the type of the object.
90  
91 =over 4
92  
93 =head2 start,stop,restart
94  
95 =back
96  
97 This methods deppends on where are installed daemontools, by
98 default, they are at /usr/local/bin/svc but you can change
99 it with the svc method.
100  
101 $cache->restart or warn "Cant restart";
102 $server->stop or warn "Cant stop";
103 $server->start or warn "Cant start";
104  
105 =over 4
106  
107 =head2 svc
108  
109 =back
110  
111 If you pass a parameter, it sets the location of svc, otherways
112 it just return the current value.
113  
114 my $svc_location = $server->svc;
115 $server->svc('/usr/bin/svc')
116 or warn "Cant set svc to /usr/bin/svc";
117  
118 =head1 AUTHOR
119  
120 Anarion: anarion@7a69ezine.org
121  
122 This library is free software; you can redistribute it and/or modify
123 it under the same terms as Perl itself.
124  
125 =head1 SEE ALSO
126  
127 L<DNS::TinyDNS::dnscache>.
128 L<DNS::TinyDNS::dnsserver>.
129  
130 =cut
131  
132 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 1 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
136  
137  
138 sub new
139 2 ▶{
Subroutine new: called CC=5 SCAR=20.3
Pod documented
140 2 my ($clase,%tipus) = @_;
141 2 ▶ unless($tipus{type} eq "dnscache" or $tipus{type} eq "dnsserver")
Branch: 1 branch
BranchTrueFalseTotal
unless ($tipus{"type"} eq "dnscache" or $tipus{"type"} eq "dnsserver") 0 2 2
Condition66%2/3
$tipus{"type"} eq "dnscache" or $tipus{"type"} eq "dnsserver"
l!l&&r!l&&!r
110
MC/DC0%0/2
$tipus{"type"} eq "dnscache" or $tipus{"type"} eq "dnsserver"
$tipus{"type"} eq "dnscache" $tipus{"type"} eq "dnsserver"
Truth table66%2/3
A or B
A= $tipus{"type"} eq "dnscache"B= $tipus{"type"} eq "dnsserver"
ABresult
1X1
011
000
142 {
143 0 carp "ERROR: Invalid type. You should use dnsserver or dnscache";
144 0 return 0;
145 }
146 2 ▶ eval qq(require ${clase}::$tipus{type})
Branch: 1 branch
BranchTrueFalseTotal
eval qq[require ${clase}::$tipus{"type"}] or carp($@) and return 0 2 0 2
Condition25%1/4
eval "require ${clase}::$tipus{'type'}" || carp($@) && (return 0)
l!l
20
carp($@) and return 0
!ll
00
MC/DC0%0/2
eval "require ${clase}::$tipus{'type'}" || carp($@) && (return 0)
eval "require ${clase}::$tipus{'type'}" carp($@)
Truth table33%1/3
A || B
A= eval "require ${clase}::$tipus{'type'}"B= carp($@)
ABresult
1X1
011
000
147 or carp $@ and return 0;
148 2 return "${clase}::$tipus{type}"->new( $tipus{dir} );
149 }
150  
151 sub dir
152 2 ▶{
Subroutine dir: called CC=3 SCAR=13.3
Pod documented
153 2 my ($self,$dir) = @_;
154 2 ▶ if ($dir)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 2 0 2
155 {
156 2 ▶ return 0 unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 0 2
157 0 $self->{dir} = $dir
158 }
159 return $self->{dir}
160 0 }
161  
162 sub get_env
163 0 ▶{
Subroutine get_env: not called CC=10 SCAR=47.0
Pod documented
164 0 my ($self,@types) = @_;
165 0 local *FILE;
166  
167 0 ▶ unless($self->{dir})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dir"}) 0 0 0
168 {
169 0 carp "ERROR: You must set the directory";
170 0 return 0;
171 }
172  
173 0 @types = map { uc } @types;
174  
175 0 for my $type (@types)
176 {
177 0 ▶ unless(exists $self->{t_env}->{$type})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"t_env"}{$type}) 0 0 0
178 {
179 0 carp "ERROR: Invalid env type";
180 0 return 0;
181 }
182  
183 # only check the fs if its not set
184 0 ▶ unless($self->{t_env}->{$type})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"t_env"}{$type}) 0 0 0
185 {
186 0 my $file = $self->{dir} . "/env/" . $type;
187 0 ▶ open(FILE,$file)
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or carp("ERROR: Cant read from $file") and return 0 0 0
Condition0%0/5
open(FILE, $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(FILE, $file) || carp("ERROR: Cant read from $file") && (return)
open(FILE, $file) carp("ERROR: Cant read from $file")
Truth table0%0/3
A || B
A= open(FILE, $file)B= carp("ERROR: Cant read from $file")
ABresult
1X1
011
000
188 or carp "ERROR: Cant read from $file" and return;
189 0 ▶ flock(FILE,LOCK_EX)
Branch: 1 branch
BranchTrueFalseTotal
flock FILE, 2 or carp("ERROR: Cant flock $file") 0 0 0
190 or carp "ERROR: Cant flock $file";
191 0 ▶ seek(FILE,0,0)
Branch: 1 branch
BranchTrueFalseTotal
seek FILE, 0, 0 or carp("ERROR: Cant seek $file") 0 0 0
192 or carp "ERROR: Cant seek $file";
193 0 chomp($self->{t_env}->{$type} = <FILE>);
194 0 ▶ close(FILE)
Branch: 1 branch
BranchTrueFalseTotal
close FILE or carp("ERROR: Cant close $file") 0 0 0
195 or carp "ERROR: Cant close $file";
196 }
197 }
198 0 return @{$self->{t_env}}{@types};
199 }
200  
201 sub set_env
202 0 ▶{
Subroutine set_env: not called CC=10 SCAR=47.0
Pod documented
203 0 my ($self,%types) = @_;
204 0 local *FILE;
205  
206 0 ▶ unless($self->{dir})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dir"}) 0 0 0
207 {
208 0 carp "ERROR: Directory not set";
209 0 return 0;
210 }
211  
212  
213 0 for my $type (keys %types)
214 {
215 0 my $uctype = uc $type;
216 0 ▶ unless(exists $self->{t_env}->{$uctype})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"t_env"}{$uctype}) 0 0 0
217 {
218 0 carp "ERROR: Invalid env type";
219 0 return 0;
220 }
221  
222 0 ▶ unless(defined $types{$type})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $types{$type}) 0 0 0
223 {
224 0 carp "ERROR: You must supply a new value for $type";
225 0 return 0;
226 }
227  
228 0 my $file = $self->{dir} . "/env/" . $uctype;
229 0 ▶ sysopen(FILE, $file, O_RDWR | O_CREAT | O_TRUNC)
Branch: 1 branch
BranchTrueFalseTotal
sysopen FILE, $file, 578 or carp("ERROR: Cant write to $file") and return 0 0 0
Condition0%0/5
sysopen(FILE, $file, 578) || 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
sysopen(FILE, $file, 578) || carp("ERROR: Cant write to $file") && (return)
sysopen(FILE, $file, 578) carp("ERROR: Cant write to $file")
Truth table0%0/3
A || B
A= sysopen(FILE, $file, 578)B= carp("ERROR: Cant write to $file")
ABresult
1X1
011
000
230 or carp "ERROR: Cant write to $file" and return;
231 0 ▶ flock(FILE,LOCK_EX)
Branch: 1 branch
BranchTrueFalseTotal
flock FILE, 2 or carp("ERROR: Cant lock $file") 0 0 0
232 or carp "ERROR: Cant lock $file";
233 0 ▶ seek(FILE,0,0)
Branch: 1 branch
BranchTrueFalseTotal
seek FILE, 0, 0 or carp("ERROR: Cant seek $file") 0 0 0
234 or carp "ERROR: Cant seek $file";
235 0 $self->{t_env}->{$uctype} = $types{$type};
236 0 syswrite(FILE,$types{$type});
237 0 ▶ close(FILE)
Branch: 1 branch
BranchTrueFalseTotal
close FILE or carp("ERROR: Cant close $file") 0 0 0
238 or carp "ERROR: Cant close $file";
239 }
240 0 return 1;
241 }
242  
243 sub stop
244 0 ▶{
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
245 0 my $self = shift;
246 0 system "$self->{svc} -d $self->{dir}";
247 0 return !$?;
248 }
249  
250 sub start
251 0 ▶{
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
252 0 my $self = shift;
253 0 system "$self->{svc} -u $self->{dir}";
254 0 return !$?;
255 }
256  
257 sub restart
258 0 ▶{
Subroutine restart: not called CC=1 SCAR=6.9
Pod documented
259 0 my $self = shift;
260 0 system "$self->{svc} -t $self->{dir}";
261 0 return !$?;
262 }
263  
264 sub svc
265 0 ▶{
Subroutine svc: not called CC=3 SCAR=24.8
Pod documented
266 0 my ($self,$svc) = @_;
267 0 ▶ if ($svc)
Branch: 1 branch
BranchTrueFalseTotal
if ($svc) 0 0 0
268 {
269 0 ▶ return 0 unless -f $svc;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $svc 0 0 0
270 0 $self->{svc} = $svc
271 }
272 return $self->{svc}
273 0 }
274  
275 1 1;
« blib/lib/DNS/TinyDNS/dnscache.pm ↑ Summary blib/lib/DNS/example.pl »