DHCPd.pm

filter: Statementstmt 77.3% 58 / 75 Branchbran 61.1% 11 / 18 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 60.0% 9 / 15 Podpod 100% 1 / 1 totaltotal 69.2% 81 / 117 CC 12 SCAR 15.0
CC
12
Cov
72%
test
9.4
_run
6.5
_build_child_obj
2.0
CRAP 4.5
« DHCPd/Config/Role.pm ↑ Summary DHCPd/Config/Key.pm »
1 package Net::ISC::DHCPd;
2  
3 =head1 NAME
4  
5 Net::ISC::DHCPd - Interacts with ISC DHCPd
6  
7 =head1 VERSION
8  
9 0.1709
10  
11 =head1 SYNOPSIS
12  
13 my $dhcpd = Net::ISC::DHCPd->new(
14 config => { file => "path/to/config" },
15 leases => { file => "path/to/leases" },
16 omapi => { key => "some key" },
17 );
18  
19 $dhcpd->config->parse;
20  
21 See the tests bundled to this distribution for more examples.
22  
23 =head1 DESCRIPTION
24  
25 This namespace contains three semi-separate projects, which this module
26 binds together: L<dhcpd.conf|Net::ISC::DHCPd::Config>,
27 L<dhcpd.leases|Net::ISC::DHCPd::Leases> and L<omapi|Net::ISC::DHCPd::OMAPI>.
28 It is written with L<Moose> which provides classes and roles to represents
29 things like a host, a lease or any other thing.
30  
31 The distribution as a whole is targeted an audience who configure and/or
32 analyze the L<Internet Systems Consortium DHCP Server|http://www.isc.org/software/dhcp>.
33 If you are not familiar with the server, check out
34 L<the man pages|http://www.google.com/search?q=man+dhcpd>.
35  
36 =cut
37  
38 6 ▶use Class::Load 0.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use Moose 0.90;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use MooseX::Types::Path::Class 0.05 qw(File);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use Net::ISC::DHCPd::Types ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 6 ▶use File::Temp 0.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 6 our $VERSION = eval '0.1709';
47  
48 =head1 ATTRIBUTES
49  
50 =head2 config
51  
52 This attribute holds a read-only L<Net::ISC::DHCPd::Config> object.
53 It can be set from the constructor, using either an object or a hash-ref.
54 The hash-ref will then be passed on to the constructor.
55  
56 =cut
57  
58 6 has config => (
59 is => 'ro',
60 isa => ConfigObject,
61 coerce => 1,
62 lazy_build => 1,
63 );
64  
65 0 ▶__PACKAGE__->meta->add_method(_build_config => sub { _build_child_obj(Config => @_) });
Subroutine __ANON__: not called CC=1 SCAR=2.6
Subroutine _build_config: not called CC=1 SCAR=2.6
66  
67 =head2 leases
68  
69 This attribute holds a read-only L<Net::ISC::DHCPd::Leases> object.
70 It can be set from the constructor, using either an object or a hash-ref.
71 The hash-ref will then be passed on to the constructor.
72  
73 =cut
74  
75 6 has leases => (
76 is => 'ro',
77 isa => LeasesObject,
78 coerce => 1,
79 lazy_build => 1,
80 );
81  
82 0 ▶__PACKAGE__->meta->add_method(_build_leases => sub { _build_child_obj(Leases => @_) });
Subroutine _build_leases: not called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=1 SCAR=2.6
83  
84 =head2 omapi
85  
86 This attribute holds a read-only L<Net::ISC::DHCPd::OMAPI> object.
87 It can be set from the constructor, using either an object or a hash-ref.
88 The hash-ref will then be passed on to the constructor.
89  
90 =cut
91  
92 6 has omapi => (
93 is => 'ro',
94 isa => OMAPIObject,
95 coerce => 1,
96 lazy_build => 1,
97 );
98  
99 0 ▶__PACKAGE__->meta->add_method(_build_omapi => sub { _build_child_obj(OMAPI => @_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
100  
101 =head2 binary
102  
103 This attribute holds a L<Path::Class::File> object to the dhcpd binary.
104 It is read-only and the default is "dhcpd3".
105  
106 =cut
107  
108 6 has binary => (
109 is => 'ro',
110 isa => File,
111 coerce => 1,
112 default => 'dhcpd3',
113 );
114  
115 =head2 errstr
116  
117 Holds the last know error as a plain string. This only applies to OMAPI.
118  
119 =cut
120  
121 6 has errstr => (
122 is => 'rw',
123 isa => 'Str',
124 default => '',
125 );
126  
127 =head1 METHODS
128  
129 =head2 parse
130  
131 $config->parse;
132  
133 This parses the config file or the leases file.
134  
135 =cut
136  
137 =head2 generate
138  
139 print $config->generate;
140  
141 This generates a copy of the config file in plaintext.
142  
143 =cut
144  
145  
146 =head2 test
147  
148 $bool = $self->test("config");
149 $bool = $self->test("leases");
150  
151 Will test either the config or leases file. It returns a boolean value
152 which indicates if it is valid or not: True means it is valid, while
153 false means it is invalid. Check L</errstr> on failure - it will contain
154 a descriptive string from either this module, C<$!> or the exit value
155 (integer stored as a string).
156  
157 =cut
158  
159 9 ▶sub test {
Subroutine test: called CC=7 SCAR=22.4
Pod documented
160 9 my $self = shift;
161 9 ▶ my $what = shift || q();
Condition50%1/2
shift() || ''
l!l
90
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
162 9 my($child_error, $errno, $output);
163  
164 9 ▶ if($what eq 'config') {
Branch: 2 branches
BranchTrueFalseTotal
if ($what eq 'config') { } 4 5 9
elsif ($what eq 'leases') { } 5 0 5
165 4 my $tmp = File::Temp->new;
166 4 print $tmp $self->config->generate;
167 4 $output = $self->_run('-t', '-cf', $tmp->filename);
168 3 ($child_error, $errno) = ($?, $!);
169 }
170 elsif($what eq 'leases') {
171 5 $output = $self->_run('-t', '-lf', $self->leases->file);
172 3 ($child_error, $errno) = ($?, $!);
173 }
174 else {
175 0 $self->errstr('Invalid argument');
176 0 return;
177 }
178  
179 # let's set this anyway...
180 6 $self->errstr($output);
181  
182 6 ▶ if($child_error and $child_error == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child_error and $child_error == -1) { } 0 6 6
elsif ($child_error) { } 6 0 6
Condition33%1/3
$child_error and $child_error == -1
!ll&&!rl&&r
060
MC/DC0%0/2
$child_error and $child_error == -1
$child_error $child_error == -1
Truth table33%1/3
A and B
A= $child_errorB= $child_error == -1
ABresult
0X0
100
111
183 0 $self->errstr($errno);
184 0 ($!, $?) = ($errno, $child_error);
185 0 return;
186 }
187 elsif($child_error) {
188 6 ($!, $?) = ($errno, $child_error);
189 6 return;
190 }
191  
192 0 return 1;
193 }
194  
195 9 ▶sub _run {
Subroutine _run: called CC=6 SCAR=18.7
196 9 my $self = shift;
197 9 my @args = @_;
198  
199 9 ▶ pipe my $reader, my $writer or return '';
Branch: 1 branch
BranchTrueFalseTotal
pipe my $reader, my $writer or return "" 9 0 9
200  
201 9 ▶ if(my $pid = fork) { # parent
Branch: 2 branches
BranchTrueFalseTotal
if (my $pid = fork) { } 6 3 9
elsif (defined $pid) { } 3 0 3
202 6 close $writer;
203 6 wait; # for child process...
204 6 local $/;
205 6 return readline $reader;
206 }
207 elsif(defined $pid) { # child
208 3 close $reader;
209 3 ▶ open STDERR, '>&', $writer or confess $!;
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $writer or confess($!) 3 0 3
210 3 ▶ open STDOUT, '>&', $writer or confess $!;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $writer or confess($!) 3 0 3
211 3 { exec $self->binary, @args }
212 0 confess "Exec() failed";
213 }
214  
215 0 return ''; # fork failed. check $!
216 }
217  
218 # used from attributes
219 0 ▶sub _build_child_obj {
Subroutine _build_child_obj: not called CC=1 SCAR=6.9
220 0 my $type = shift;
221 0 my $self = shift;
222  
223 0 Class::Load::load_class("Net::ISC::DHCPd::$type");
224  
225 0 return "Net::ISC::DHCPd::$type"->new(@_);
226 }
227  
228 =head1 SEE ALSO
229  
230 =over 8
231  
232 =item Net::DHCP::Info
233  
234 C<Net::DHCP::Info> an older dhcpd.leases and dhcpd.conf parser, also written
235 by Jan Henning Thorsen. It has many limitations that these modules address.
236  
237 =item kea.isc.org
238  
239 C<kea.isc.org> a replacement DHCP server from ISC that is high-performance and
240 supports an API and on-line configuration directly.
241  
242 =back
243  
244 =head1 BUGS
245  
246 Please report any bugs or feature requests to
247 C<bug-net-isc-dhcpd at rt.cpan.org>, or through the web interface at
248 L<https://github.com/jhthorsen/net-isc-dhcpd/issues>.
249 I will be notified, and then you'll automatically be notified of progress on
250 your bug as I make changes.
251  
252 =head1 COPYRIGHT & LICENSE
253  
254 Copyright 2007 Jan Henning Thorsen, all rights reserved.
255  
256 This program is free software; you can redistribute it and/or modify it
257 under the same terms as Perl itself.
258  
259 =head1 AUTHOR
260  
261 Jan Henning Thorsen, C<< <jhthorsen at cpan.org> >>
262  
263 =head1 MAINTAINER
264  
265 Robert Drake, C<< <rdrake at cpan.org> >>
266  
267 =head1 CONTRIBUTORS
268  
269 Nito Martinez
270  
271 Alexey Illarionov
272  
273 Patrick
274  
275 napetrov
276  
277 zoffixznet
278  
279 Bossi
280  
281 =cut
282 6 __PACKAGE__->meta->make_immutable;
283 6 1;
« DHCPd/Config/Role.pm ↑ Summary DHCPd/Config/Key.pm »