Utilities/Options.pm

filter: Statementstmt 40.2% 35 / 87 Branchbran 18.7% 9 / 48 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 58.3% 7 / 12 Podpod 87.5% 7 / 8 totaltotal 36.2% 58 / 160 CC 27 SCAR 28.9
CC
27
Cov
32%
validate_option
23.1
get_code
20.0
get_long
20.0
CRAP 18.0
« Utilities/Subnet.pm ↑ Summary Utilities/INI_loader.pm »
1 package Net::DHCP::Config::Utilities::Options;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Net::CIDR;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::DHCP::Config::Utilities::Options - Helper utilities for working with DHCP options.
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 5 our $VERSION = '0.0.1';
19  
20  
21 =head1 SYNOPSIS
22  
23 use Net::DHCP::Config::Utilities::Options;
24
25 my $dhcp_options=Net::DHCP::Config::Utilities::Options->new;
26
27 my $options=$dhcp_options->get_options;
28 use Data::Dumper;
29 print Dumper( $options );
30  
31 my $error=$dhcp_options->validate_option( 'dns', '192.168.0.1 , 10.10.10.10' );
32 if ( defined( $error ) ){
33 die( $error );
34 }
35  
36 =head1 METHODS
37  
38 =head2 new
39  
40 Initiates the object.
41  
42 my $dhcp_options=Net::DHCP::Config::Utilities::Options->new;
43  
44 =cut
45  
46 sub new {
47 8 ▶ my $self={
Subroutine new: called CC=1 SCAR=0.0
Pod documented
48 options=>{
49 'mask'=>{
50 'code'=>'0',
51 'multiple'=>'0',
52 'type'=>'ip',
53 'long'=>'subnet-mask',
54 },
55 'time-offset'=>{
56 'code'=>'1',
57 'multiple'=>'0',
58 'type'=>'int',
59 'long'=>'time-offset',
60 },
61 'routers'=>{
62 'code'=>'3',
63 'multiple'=>'1',
64 'type'=>'ip',
65 'long'=>'routers',
66 },
67 'ntp'=>{
68 'code'=>'4',
69 'multiple'=>'1',
70 'type'=>'ip',
71 'long'=>'time-servers',
72 },
73 'dns'=>{
74 'code'=>'6',
75 'multiple'=>'1',
76 'type'=>'ip',
77 'long'=>'domain-name-servers',
78 },
79 'root'=>{
80 'code'=>'17',
81 'multiple'=>'0',
82 'type'=>'txt',
83 'long'=>'root-path',
84 },
85 'mtu'=>{
86 'code'=>'26',
87 'multiple'=>'0',
88 'type'=>'int',
89 'long'=>'interface-mtu',
90 },
91 'broadcast'=>{
92 'code'=>'28',
93 'multiple'=>'0',
94 'type'=>'ip',
95 'long'=>'broadcast-address',
96 },
97 'lease-time'=>{
98 'code'=>'51',
99 'multiple'=>'0',
100 'type'=>'int',
101 'long'=>'dhcp-lease-time',
102 },
103 'tftp-server'=>{
104 'code'=>'66',
105 'multiple'=>'0',
106 'type'=>'txt',
107 'long'=>'next-server',
108 },
109 'bootfile'=>{
110 'code'=>'67',
111 'multiple'=>'0',
112 'type'=>'txt',
113 'long'=>'filename',
114 },
115 'v4-access-domain'=>{
116 'code'=>'213',
117 'multiple'=>'0',
118 'type'=>'txt',
119 'long'=>'v4-access-domain',
120 },
121 'web-proxy'=>{
122 'code'=>'252',
123 'multiple'=>'0',
124 'type'=>'txt',
125 'long'=>'web-rpoxy',
126 },
127 },
128 long_to_short=>{
129 'filename'=>'bootfile',
130 'next-server'=>'tftp-server',
131 'dhcp-lease-time'=>'lease-time',
132 'interface-mtr'=>'mtu',
133 'root-path'=>'root',
134 'domain-name-servers'=>'dns',
135 'time-servers'=>'ntp',
136 'broadcast-address'=>'broadcast',
137 'subnet-mask'=>'mask',
138 },
139 };
140 8 bless $self;
141  
142 8 return $self;
143 }
144  
145 =head2 get_code
146  
147 Returns the DHCP code value for a option.
148  
149 One option is taken and that is the option name.
150  
151 If the option name is not found or is undef,
152 then undef is returned.
153  
154 # you can use the long name
155 print 'subnet-mask: '.$dhcp_options->get_code('subnet-mask')."\n";
156 # or the easier to remember short name
157 print 'mask: '.$dhcp_options->get_code('mask')."\n";
158  
159 =cut
160  
161 sub get_code{
162 0 ▶ my $self=$_[0];
Subroutine get_code: not called CC=4 SCAR=30.0
Pod documented
163 0 my $option=$_[1];
164  
165 # need a value to proceed
166 0 ▶ if ( !defined( $option ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option) 0 0 0
167 0 return undef;
168 }
169  
170 # if we find this, grab the short version
171 0 ▶ if ( defined( $self->{long_to_short}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"long_to_short"}{$option}) 0 0 0
172 0 $option=$self->{long_to_short}{$option};
173 }
174  
175 0 ▶ if ( !defined( $self->{options}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{$option}) 0 0 0
176 0 return undef;
177 }
178  
179 0 return $self->{options}{$option}{code};
180 }
181  
182 =head2 get_long
183  
184 Returns the long option name for the specified option.
185  
186 One argument is taken and that is the option name.
187  
188 If the option name is not found or is undef,
189 then undef is returned.
190  
191 print 'root: '.$dhcp_options->get_long('root')."\n";
192 print 'mask: '.$dhcp_options->get_long('mask')."\n";
193 print 'mtu: '.$dhcp_options->get_long('mtu')."\n";
194 print 'routers: '.$dhcp_options->get_long('routers')."\n";
195  
196 =cut
197  
198 sub get_long{
199 0 ▶ my $self=$_[0];
Subroutine get_long: not called CC=4 SCAR=30.0
Pod documented
200 0 my $option=$_[1];
201  
202 # need a value to proceed
203 0 ▶ if ( !defined( $option ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option) 0 0 0
204 0 return undef;
205 }
206  
207 # if we find this, grab the short version
208 0 ▶ if ( defined( $self->{long_to_short}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"long_to_short"}{$option}) 0 0 0
209 0 $option=$self->{long_to_short}{$option};
210 }
211  
212 0 ▶ if ( !defined( $self->{options}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{$option}) 0 0 0
213 0 return undef;
214 }
215  
216 0 return $self->{options}{$option}{long};
217 }
218  
219  
220 =head2 get_multiple
221  
222 Returns if multiple values are supported by this option.
223  
224 0 = single value
225 1 = multiple values
226  
227 One option is taken and that is the option name.
228  
229 If the option name is not found or is undef,
230 then undef is returned.
231  
232 # you can use the long name
233 print 'subnet-mask: '.$dhcp_options->get_multiple('subnet-mask')."\n";
234 # or the easier to remember short name
235 print 'mask: '.$dhcp_options->get_multiple('mask')."\n";
236  
237 if ( $dhcp_options->get_multiple('dns') ){
238 print "Multiple values are supported... exanple\n".
239 "10.10.10.1 , 10.10.10.2\n";
240 }
241  
242 =cut
243  
244 sub get_multiple{
245 0 ▶ my $self=$_[0];
Subroutine get_multiple: not called CC=4 SCAR=30.0
Pod documented
246 0 my $option=$_[1];
247  
248 # need a value to proceed
249 0 ▶ if ( !defined( $option ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option) 0 0 0
250 0 return undef;
251 }
252  
253 # if we find this, grab the short version
254 0 ▶ if ( defined( $self->{long_to_short}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"long_to_short"}{$option}) 0 0 0
255 0 $option=$self->{long_to_short}{$option};
256 }
257  
258 0 ▶ if ( !defined( $self->{options}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{$option}) 0 0 0
259 0 return undef;
260 }
261  
262 0 return $self->{options}{$option}{multiple};
263 }
264  
265 =head2 get_options
266  
267 Returns a hash ref with the various options.
268  
269 my $options=$dhcp_options->get_options;
270 foreach my $opt ( keys( %{ $options } ) ){
271 print "----\n".
272 "option: ".$opt."\n".
273 "code: ".$options->{$opt}{'code'}."\n".
274 "multiple: ".$options->{$opt}{'multiple'}."\n".
275 "type: ".$options->{$opt}{'type'}."\n".
276 "long: ".$options->{$opt}{'long'}."\n".
277 }
278  
279 =cut
280  
281 sub get_options{
282 8 ▶ return $_[0]->{options};
Subroutine get_options: called CC=1 SCAR=0.0
Pod documented
283 }
284  
285 =head2 get_type
286  
287 Returns the data type that the option in question is.
288  
289 ip = IP address
290 int = integer
291 txt = text field that must be defined
292  
293 One option is taken and that is the option name.
294  
295 If the option name is not found or is undef,
296 then undef is returned.
297  
298 print 'root: '.$dhcp_options->get_type('root')."\n";
299 print 'mask: '.$dhcp_options->get_type('mask')."\n";
300 print 'mtu: '.$dhcp_options->get_type('mtu')."\n";
301  
302 =cut
303  
304 sub get_type{
305 0 ▶ my $self=$_[0];
Subroutine get_type: not called CC=4 SCAR=30.0
Pod documented
306 0 my $option=$_[1];
307  
308 # need a value to proceed
309 0 ▶ if ( !defined( $option ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option) 0 0 0
310 0 return undef;
311 }
312  
313 # if we find this, grab the short version
314 0 ▶ if ( defined( $self->{long_to_short}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"long_to_short"}{$option}) 0 0 0
315 0 $option=$self->{long_to_short}{$option};
316 }
317  
318 0 ▶ if ( !defined( $self->{options}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{$option}) 0 0 0
319 0 return undef;
320 }
321  
322 0 return $self->{options}{$option}{type};
323 }
324  
325 =head2 valid_option_name
326  
327 This checks if the option name is valid.
328  
329 This checks for possible long and short forms.
330  
331 if ( ! $dhcp_options->valid_option_name( $option ) ){
332 die( $option.' is not a valid option' );
333 }
334  
335  
336 =cut
337  
338 sub valid_option_name{
339 0 ▶ my $self=$_[0];
Subroutine valid_option_name: not called CC=4 SCAR=30.0
Pod documented
340 0 my $option=$_[1];
341  
342 0 ▶ if ( ! defined( $option ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option) 0 0 0
343 0 return undef;
344 }
345  
346 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{$option} or defined $self->{"long_to_short"}{$option}) 0 0 0
Condition0%0/3
defined $self->{"options"}{$option} or defined $self->{"long_to_short"}{$option}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $self->{"options"}{$option} or defined $self->{"long_to_short"}{$option}
defined $self->{"options"}{$option} defined $self->{"long_to_short"}{$option}
Truth table0%0/3
A or B
A= defined $self->{"options"}{$option}B= defined $self->{"long_to_short"}{$option}
ABresult
1X1
011
000
347 ( defined( $self->{options}{$option} ) ) ||
348 ( defined( $self->{long_to_short}{$option} ) )
349 ){
350 0 return 1;
351 }
352  
353 0 return undef;
354 }
355  
356 =head2 validate_options
357  
358 This validates a option and the value for it.
359  
360 Twu arguments are taken. The first is the option name
361 and the third is the value.
362  
363 If any issues are found a string is returned that describes it.
364  
365 If there are no issues undef is returned.
366  
367 This should not be mistaken for sanity checking. This just
368 makes sure that the data is the correct type for the option.
369  
370 my $error=$dhcp_options->validate_option( $option, $value );
371 if ( defined( $error ) ){
372 die( $error );
373 }
374  
375 =cut
376  
377 sub validate_option{
378 16 ▶ my $self=$_[0];
Subroutine validate_option: called CC=12 SCAR=31.4
Pod undocumented
379 16 my $option=$_[1];
380 16 my $value=$_[2];
381  
382 # need a value to proceed
383 16 ▶ if ( !defined( $option ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option) 0 16 16
384 0 return 'Option undefined';
385 }
386  
387 # need a value to proceed
388 16 ▶ if ( !defined( $value ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 16 16
389 0 return 'Option Value undefined';
390 }
391  
392 # if we find this, grab the short version
393 16 ▶ if ( defined( $self->{long_to_short}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"long_to_short"}{$option}) 0 16 16
394 0 $option=$self->{long_to_short}{$option};
395 }
396  
397 # if this hits, then we don't have a valid name
398 16 ▶ if ( !defined( $self->{options}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{$option}) 0 16 16
399 0 return '"'.$option.'" was is not a valid option name';
400 }
401  
402 16 my $type=$self->{options}{$option}{type};
403  
404 # if it is txt type, we have already checked to make sure
405 # it is defined
406 16 ▶ if ( $type eq 'txt' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "txt") 0 16 16
407 0 return undef;
408 }
409  
410 # trans form it into a array to simply processing
411 16 my @values;
412 16 ▶ if ( $self->{options}{$option}{multiple} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{$option}{'multiple'}) { } 16 0 16
413 16 @values=split( /\ *\,\ */, $value );
414 }else{
415 # multiple values are not taken, just shove the
416 # value into the array
417 0 push( @values, $value );
418 }
419  
420 16 foreach my $test_value ( @values ){
421 24 ▶ if ( $type eq 'int' ){
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'int') { } 0 24 24
elsif ($type eq 'ip') { } 24 0 24
422 0 ▶ if ( $test_value !~ /^[0-9]+$/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($test_value =~ /^[0-9]+$/) 0 0 0
423 0 return "'".$test_value."' is not a valid integer";
424 }
425 }elsif( $type eq 'ip' ){
426 24 eval{
427 24 my @cidrs=Net::CIDR::addr2cidr($test_value);
428 };
429 24 ▶ if ( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
430 0 return "'".$test_value."' is not a valid IP";
431 }
432 }
433 }
434  
435 16 return undef;
436 }
437  
438 =head1 SUPPORT OPTIONS
439  
440 This only supports the more commonly used one for now and avoids the out of date ones.
441  
442 | Code | Name | Multi | Type | Long Name |
443 |------|------------------|-------|------|---------------------|
444 | 0 | mask | 0 | IP | subnet-mask |
445 | 1 | time-offset | 0 | INT | time-offset |
446 | 3 | routers | 1 | IP | routers |
447 | 4 | ntp | 1 | IP | time-servers |
448 | 6 | dns | 1 | IP | domain-name-servers |
449 | 17 | root | 0 | TXT | root-path |
450 | 26 | mtu | 0 | INT | interface-mtu |
451 | 28 | broadcast | 0 | IP | broadcast-address |
452 | 51 | lease-time | 0 | INT | dhcp-lease-time |
453 | 66 | tfp-server | 0 | TXT | next-server |
454 | 67 | bootfile | 0 | TXT | filename |
455 | 213 | v4-access-domain | 0 | TXT | v4-access-domain |
456 | 252 | web-proxy | 0 | TXT | web-proxy |
457  
458 For options that can take multiple values, /\ *\,\ */ is used for the split.
459  
460 Validation is done as below.
461  
462 INT = /^[0-9]+$/
463 IP = If Net::CIDR::addr2cidr can make sense of it.
464 TXT = defined
465  
466 =head1 AUTHOR
467  
468 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
469  
470 =head1 BUGS
471  
472 Please report any bugs or feature requests to C<bug-net-dhcp-config-utilities at rt.cpan.org>, or through
473 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
474 automatically be notified of progress on your bug as I make changes.
475  
476  
477  
478  
479 =head1 SUPPORT
480  
481 You can find documentation for this module with the perldoc command.
482  
483 perldoc Net::DHCP::Config::Utilities
484  
485  
486 You can also look for information at:
487  
488 =over 4
489  
490 =item * RT: CPAN's request tracker (report bugs here)
491  
492 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-DHCP-Config-Utilities>
493  
494 =item * AnnoCPAN: Annotated CPAN documentation
495  
496 L<http://annocpan.org/dist/Net-DHCP-Config-Utilities>
497  
498 =item * CPAN Ratings
499  
500 L<https://cpanratings.perl.org/d/Net-DHCP-Config-Utilities>
501  
502 =item * Search CPAN
503  
504 L<https://metacpan.org/release/Net-DHCP-Config-Utilities>
505  
506 =back
507  
508  
509 =head1 ACKNOWLEDGEMENTS
510  
511  
512 =head1 LICENSE AND COPYRIGHT
513  
514 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
515  
516 This is free software, licensed under:
517  
518 The Artistic License 2.0 (GPL Compatible)
519  
520  
521 =cut
522  
523 5 1; # End of Net::DHCP::Config::Utilities
« Utilities/Subnet.pm ↑ Summary Utilities/INI_loader.pm »