Utilities/Subnet.pm

filter: Statementstmt 61.2% 60 / 98 Branchbran 40.0% 16 / 40 Conditioncond 58.3% 7 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 66.6% 10 / 15 Podpod 100% 9 / 9 totaltotal 56.3% 102 / 181 CC 27 SCAR 30.7
CC
27
Cov
55%
option_set
42.0
new
29.0
option_get
12.0
CRAP 21.5
↑ Summary Utilities/Options.pm »
1 package Net::DHCP::Config::Utilities::Subnet;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Net::CIDR;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Net::DHCP::Config::Utilities::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Net::CIDR::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::DHCP::Config::Utilities::Subnet - Represents a subnet.
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 4 our $VERSION = '0.0.1';
21  
22  
23 =head1 SYNOPSIS
24  
25 use Net::DHCP::Config::Utilities::Subnet;
26  
27 my $options={
28 base=>'10.0.0.0',
29 mask=>'255.255.255.0',
30 dns=>'10.0.0.1 , 10.0.10.1',
31 desc=>'a example subnet',
32 };
33
34 my $subnet = Net::DHCP::Config::Utilities::Subnet->new( $options );
35  
36  
37 =head1 METHODS
38  
39 =head2 new
40  
41 This initiates the object.
42  
43 my $options={
44 base=>'10.0.0.0',
45 mask=>'255.255.255.0',
46 dns=>'10.0.0.1 , 10.0.10.1',
47 desc=>'a example subnet',
48 };
49
50 my $subnet = Net::DHCP::Config::Utilities::Subnet->new( $options );
51  
52 =cut
53  
54 sub new {
55 8 ▶ my %args;
Subroutine new: called CC=20 SCAR=33.7
Pod documented
56 8 ▶ if ( defined( $_[1] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 8 0 8
57 8 %args=%{$_[1]};
58 }
59  
60 # make sure we have the bare minimum to succeed
61 8 ▶ if ( !defined( $args{base} ) ){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $args{'base'}) { } 0 8 8
elsif (not defined $args{'mask'}) { } 0 8 8
62 0 die('No base defined');
63 }elsif( !defined( $args{mask} ) ){
64 0 die('No mask defined');
65 }
66  
67 # make sure the base and mask are sane
68 8 my $cidr;
69 8 eval{
70 8 $cidr=Net::CIDR::addrandmask2cidr( $args{base}, $args{mask} );
71 };
72 8 ▶ if ( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
73 0 die( 'Base/mask validation failed with... '.$@ );
74 }
75  
76 8 ▶ if (!defined( $args{desc} )){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"desc"}) 3 5 8
77 3 $args{desc}='';
78 }
79  
80 my $self={
81 ranges=>[],
82 desc=>$args{desc},
83 base=>$args{base},
84 mask=>$args{mask},
85 8 cidr=>$cidr,
86 options=>{},
87 };
88 8 bless $self;
89  
90 # process any specified ranges
91 8 ▶ if (defined( $args{ranges} )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ranges"}) 3 5 8
92 3 my $cidr_checker=Net::CIDR::Set->new( $cidr );
93  
94 3 foreach my $range ( @{ $args{ranges} } ){
95 3 my @range_split=split(/\ +/, $range);
96  
97 # make sure we have both start and end
98 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $range_split[0] and defined $range_split[1]) 0 3 3
Condition33%1/3
defined $range_split[0] and defined $range_split[1]
!ll&&!rl&&r
003
MC/DC0%0/2
defined $range_split[0] and defined $range_split[1]
defined $range_split[0] defined $range_split[1]
Truth table33%1/3
A and B
A= defined $range_split[0]B= defined $range_split[1]
ABresult
0X0
100
111
99 (!defined( $range_split[0] )) ||
100 (!defined( $range_split[1] ))
101 ){
102 0 die('"'.$range.'" not a properly formed range... Should be "IP IP"');
103 }
104  
105 # make sure both the top and bottom of the range are in our subnet
106 3 my @cidr_list = Net::CIDR::addr2cidr( $range_split[0] );
107 3 ▶ if (! $cidr_checker->contains_all( $cidr_list[0] ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($cidr_checker->contains_all($cidr_list[0])) 0 3 3
108 0 die('"'.$range_split[0].'" for "'.$range.'" not in the CIDR "'.$cidr.'"');
109 }
110 3 @cidr_list = Net::CIDR::addr2cidr( $range_split[1] );
111 3 ▶ if (! $cidr_checker->contains_all( $cidr_list[0] ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($cidr_checker->contains_all($cidr_list[0])) 0 3 3
112 0 die('"'.$range_split[1].'" for "'.$range.'" not in the CIDR "'.$cidr.'"');
113 }
114  
115 # if we get here, it validated and is safe to add
116 3 push( @{ $self->{ranges} }, $range );
117 }
118 }
119  
120 8 my $options_helper=Net::DHCP::Config::Utilities::Options->new;
121 8 my $options=$options_helper->get_options;
122 8 delete( $options->{mask} ); # already handled this previously
123 8 foreach my $key ( keys( %{ $options } ) ){
124 96 my $opt=$key;
125  
126 # make sure we don't have long and short, if long is different than short
127 96 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$key} and (defined $args{$options->{$key}{"long"}} and $args{$key} ne $args{$options->{$key}{"long"}})) 0 96 96
Condition66%4/6
defined $args{$key} and (defined $args{$options->{$key}{"long"}} and $args{$key} ne $args{$options->{$key}{"long"}})
!ll&&!rl&&r
80160
defined $args{$options->{$key}{"long"}} and $args{$key} ne $args{$options->{$key}{"long"}}
!ll&&!rl&&r
880
MC/DC0%0/3
defined $args{$key} and (defined $args{$options->{$key}{"long"}} and $args{$key} ne $args{$options->{$key}{"long"}})
defined $args{$key} defined $args{$options->{$key}{"long"}} $args{$key} ne $args{$options->{$key}{"long"}}
Truth table50%2/4
A and B and C
A= defined $args{$key}B= defined $args{$options->{$key}{"long"}}C= $args{$key} ne $args{$options->{$key}{"long"}}
ABCresult
0XX0
10X0
1100
1111
128 defined( $args{ $key } ) &&
129 (
130 defined( $args{ $options->{$key}{long} } ) &&
131 ( $args{ $key } ne $args{ $options->{$key}{long} } )
132 )
133 ){
134 0 die( '"'.$key.'" and "'.$args{ $options->{$key}{long} }.'" both defined and the desired one to use is ambigous' );
135 }
136  
137 # figure out if we are using long or short and set $opt appropriately
138 96 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$options->{$key}{"long"}} and not defined $args{$key}) 0 96 96
Condition66%2/3
defined $args{$options->{$key}{"long"}} and not defined $args{$key}
!ll&&!rl&&r
8880
MC/DC0%0/2
defined $args{$options->{$key}{"long"}} and not defined $args{$key}
defined $args{$options->{$key}{"long"}} not defined $args{$key}
Truth table66%2/3
A and not(B)
A= defined $args{$options->{$key}{"long"}}B= not defined $args{$key}
ABresult
0X0
100
111
139 defined( $args{ $options->{$key}{long} } ) &&
140 ( ! defined( $args{ $key } ) )
141 ){
142 $opt=$options->{$key}{long}
143 0 }
144  
145 # finally get around to processing it if we have it
146 96 ▶ if ( defined( $args{ $opt } ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$opt}) 16 80 96
147 # make sure the value for the option is sane
148 16 my $error=$options_helper->validate_option( $opt, $args{ $opt } );
149 16 ▶ if ( defined( $error ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 16 16
150 0 die('"'.$opt.'" option with value "'.$args{$opt}.'" did not validate... '.$error);
151 }
152 }
153  
154 # if we get here, it validated and is safe to add
155 # use $key so we are always saving it here as the short option
156 96 $self->{options}{$key}=$args{$opt};
157 }
158  
159 8 return $self;
160 }
161  
162 =head2 base_get
163  
164 This returns the base IP for the subnet.
165  
166 my $base_IP=$subnet->base;
167  
168 =cut
169  
170 sub base_get{
171 8 ▶ return $_[0]->{base};
Subroutine base_get: called CC=1 SCAR=0.0
Pod documented
172 }
173  
174 =head2 cidr
175  
176 Returns the CIDR for the subnet.
177  
178 my $cidr=$subnet->cidr;
179  
180 =cut
181  
182 sub cidr{
183 7 ▶ return $_[0]->{cidr};
Subroutine cidr: called CC=1 SCAR=0.0
Pod documented
184 }
185  
186 =head2 desc_get
187  
188 Returns the description.
189  
190 If this was not defined when initialized, '' will be returned.
191  
192 my $desc=$subnet->desc_get;
193  
194 =cut
195  
196 sub desc_get{
197 0 ▶ return $_[0]->{desc};
Subroutine desc_get: not called CC=1 SCAR=6.9
Pod documented
198 }
199  
200 =head2 mask_get
201  
202 This returns the current subnet mask.
203  
204 my $mask=$subnet->mask;
205  
206 =cut
207  
208 sub mask_get{
209 9 ▶ return $_[0]->{mask};
Subroutine mask_get: called CC=1 SCAR=0.0
Pod documented
210 }
211  
212 =head2 option_get
213  
214 This returns the requested option.
215  
216 If the requested option is not set, undef is returned.
217  
218 Options are always saved internally using the short name, so if an
219 option has both a long name and shortname, then the short name is used.
220  
221 my $option_value=$subnet->option_get( $option );
222 if ( !defined( $option_value ) ){
223 print $option." is not set\n";
224 }
225  
226 =cut
227  
228 sub option_get{
229 0 ▶ my $self=$_[0];
Subroutine option_get: not called CC=3 SCAR=24.8
Pod documented
230 0 my $option=$_[1];
231  
232 # this is one that may potentially be requested, but is stored else where
233 0 ▶ if ( $option eq 'mask' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($option eq "mask") 0 0 0
234 0 return $self->{mask};
235 }
236  
237 0 ▶ if ( defined( $self->{options}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{$option}) 0 0 0
238 0 return $self->{options}{$option}
239 }
240  
241 0 return undef;
242 }
243  
244 =head2 options_list
245  
246 This list options that have been set, excluding mask.
247  
248 my @options=$subnet->options_list;
249  
250 =cut
251  
252 sub options_list{
253 0 ▶ return keys( %{ $_[0]->{options} } );
Subroutine options_list: not called CC=1 SCAR=6.9
Pod documented
254 }
255  
256 =head2 option_set
257  
258 This sets an option.
259  
260 Two arguments are taken. The first is the option
261 and the second is the value. If the value is left undefined,
262 then the option is deleted.
263  
264 eval{
265 $subnet->option_set( $option, $value );
266 };
267 if ( $@ ){
268 warn( 'Failed to set option "'.$option.'" with value "'.$value.'"... error='.$@ );
269 }
270  
271 =cut
272  
273 sub option_set{
274 0 ▶ my $self=$_[0];
Subroutine option_set: not called CC=6 SCAR=37.4
Pod documented
275 0 my $option=$_[1];
276 0 my $value=$_[2];
277  
278 0 ▶ if ( !defined( $option ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option) 0 0 0
279 0 die( 'No option defined' );
280 }
281  
282 0 ▶ if ( $option eq 'mask' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($option eq "mask") 0 0 0
283 0 die( 'Setting subnet mask here is not supported') ;
284 }
285  
286 # if no value is defined, delete the requested option
287 0 ▶ if ( ! defined( $value ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
288 0 ▶ if ( defined( $self->{options}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{$option}) 0 0 0
289 0 delete( $self->{options}{$option} );
290 }
291 0 return 1;
292 }
293  
294 # make sure the specified value is valid
295 0 my $options_helper=Net::DHCP::Config::Utilities::Options->new;
296 0 my $error=$options_helper->validate_option( $option, $value );
297 0 ▶ if ( defined( $error ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
298 0 die('"'.$option.'" option with value "'.$value.'" did not validate... '.$error);
299 }
300  
301 0 $self->{options}{$option}=$value;
302  
303 0 return 1;
304 }
305  
306 =head2 range_get
307  
308 This returns a array with containing the ranges in questions.
309  
310 my @ranges=$subnet->get_ranges;
311 foreach my $range ( @range ){
312 print "range ".$range.";\n"
313 }
314  
315 =cut
316  
317 sub range_get{
318 0 ▶ return @{ $_[0]->{ranges} };
Subroutine range_get: not called CC=1 SCAR=6.9
Pod documented
319 }
320  
321 =head1 AUTHOR
322  
323 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
324  
325 =head1 BUGS
326  
327 Please report any bugs or feature requests to C<bug-net-dhcp-config-utilities at rt.cpan.org>, or through
328 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-DHCP-Config-Utilities>. I will be notified, and then you'll
329 automatically be notified of progress on your bug as I make changes.
330  
331  
332  
333  
334 =head1 SUPPORT
335  
336 You can find documentation for this module with the perldoc command.
337  
338 perldoc Net::DHCP::Config::Utilities
339  
340  
341 You can also look for information at:
342  
343 =over 4
344  
345 =item * RT: CPAN's request tracker (report bugs here)
346  
347 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-DHCP-Config-Utilities>
348  
349 =item * AnnoCPAN: Annotated CPAN documentation
350  
351 L<http://annocpan.org/dist/Net-DHCP-Config-Utilities>
352  
353 =item * CPAN Ratings
354  
355 L<https://cpanratings.perl.org/d/Net-DHCP-Config-Utilities>
356  
357 =item * Search CPAN
358  
359 L<https://metacpan.org/release/Net-DHCP-Config-Utilities>
360  
361 =back
362  
363  
364 =head1 ACKNOWLEDGEMENTS
365  
366  
367 =head1 LICENSE AND COPYRIGHT
368  
369 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
370  
371 This is free software, licensed under:
372  
373 The Artistic License 2.0 (GPL Compatible)
374  
375  
376 =cut
377  
378 4 1; # End of Net::DHCP::Config::Utilities
↑ Summary Utilities/Options.pm »