line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::DHCP::Config::Utilities::INI_loader; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
58566
|
use 5.006; |
|
3
|
|
|
|
|
18
|
|
4
|
3
|
|
|
3
|
|
15
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
51
|
|
5
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
86
|
|
6
|
3
|
|
|
3
|
|
1264
|
use Config::Tiny; |
|
3
|
|
|
|
|
2699
|
|
|
3
|
|
|
|
|
80
|
|
7
|
3
|
|
|
3
|
|
1317
|
use File::Find::Rule; |
|
3
|
|
|
|
|
21107
|
|
|
3
|
|
|
|
|
60
|
|
8
|
3
|
|
|
3
|
|
1564
|
use Net::DHCP::Config::Utilities::Subnet; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
1495
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Net::DHCP::Config::Utilities::INI_loader - Loads subnet configurations from a INI file. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Version 0.0.1 |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.0.1'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSIS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use Net::DHCP::Config::Utilities::INI_loader; |
26
|
|
|
|
|
|
|
use Net::DHCP::Config::Utilities; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $dhcp_util = Net::DHCP::Config::Utilities->new; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $loader = Net::DHCP::Config::Utilities::INI_loader->new( $dhcp_util ); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
eval{ |
33
|
|
|
|
|
|
|
$loader->load_file( $file ); |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
if ( $@ ){ |
36
|
|
|
|
|
|
|
# do something upon error |
37
|
|
|
|
|
|
|
die( $@ ); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 METHODS |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head2 new |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
This initiates the object. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
One argument is required and that is a Net::DHCP::Config::Utilities |
47
|
|
|
|
|
|
|
object to operate on. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
use Net::DHCP::Config::Utilities; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $dhcp_util = Net::DHCP::Config::Utilities->new; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $loader = Net::DHCP::Config::Utilities::INI_loader->new( $dhcp_util ); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub new { |
58
|
3
|
|
|
3
|
1
|
52
|
my $obj=$_[1]; |
59
|
|
|
|
|
|
|
|
60
|
3
|
50
|
|
|
|
20
|
if ( ref( $obj ) ne 'Net::DHCP::Config::Utilities' ){ |
61
|
0
|
|
|
|
|
0
|
die( 'No Net::DHCP::Config::Utilities object passed or wrong ref type' ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
3
|
|
|
|
|
16
|
my $self={ |
65
|
|
|
|
|
|
|
obj=>$obj, |
66
|
|
|
|
|
|
|
}; |
67
|
3
|
|
|
|
|
8
|
bless $self; |
68
|
|
|
|
|
|
|
|
69
|
3
|
|
|
|
|
29
|
return $self; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 load_file |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
This loads a specific file in question. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
One argument is taken and that is the path to the INI file. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
If this encounter any errors, it will die. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
eval{ |
81
|
|
|
|
|
|
|
$load->load_file( $file ); |
82
|
|
|
|
|
|
|
}; |
83
|
|
|
|
|
|
|
if ( $@ ){ |
84
|
|
|
|
|
|
|
# do something upon error |
85
|
|
|
|
|
|
|
die( $@ ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub load_file{ |
91
|
8
|
|
|
8
|
1
|
1264
|
my $self=$_[0]; |
92
|
8
|
|
|
|
|
13
|
my $file=$_[1]; |
93
|
|
|
|
|
|
|
|
94
|
8
|
50
|
|
|
|
148
|
if ( ! defined( $file ) ){ |
|
|
50
|
|
|
|
|
|
95
|
0
|
|
|
|
|
0
|
die( 'No file specified' ); |
96
|
|
|
|
|
|
|
}elsif( ! -f $file ){ |
97
|
0
|
|
|
|
|
0
|
die( '"'.$file.'" is not a file or does not exist' ); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
8
|
|
|
|
|
63
|
my $ini = Config::Tiny->read( $file ); |
101
|
8
|
50
|
|
|
|
1363
|
if ( ! defined( $ini ) ){ |
102
|
0
|
|
|
|
|
0
|
die( 'Failed to read "'.$file.'"' ); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
8
|
|
|
|
|
12
|
foreach my $key ( keys( %{ $ini } ) ){ |
|
8
|
|
|
|
|
40
|
|
106
|
8
|
50
|
|
|
|
27
|
if ( $key ne '_' ){ |
107
|
|
|
|
|
|
|
# set base here, incase it is explicitly set later in the section |
108
|
8
|
|
|
|
|
20
|
my $options={ |
109
|
|
|
|
|
|
|
base=>$key, |
110
|
|
|
|
|
|
|
}; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# process each one as we need to real in the range variables |
113
|
8
|
|
|
|
|
15
|
foreach my $option_key ( keys( %{ $ini->{$key} } ) ){ |
|
8
|
|
|
|
|
21
|
|
114
|
35
|
100
|
|
|
|
71
|
if ( $option_key =~ /^range/ ){ |
115
|
|
|
|
|
|
|
# each range variable needs to be treated specially |
116
|
|
|
|
|
|
|
# as they all need loaded into a array |
117
|
3
|
50
|
|
|
|
10
|
if ( !defined $options->{ranges} ){ |
118
|
3
|
|
|
|
|
12
|
$options->{ranges}=[ $ini->{$key}{$option_key} ]; |
119
|
|
|
|
|
|
|
}else{ |
120
|
0
|
|
|
|
|
0
|
push( @{ $options->{ranges} }, $ini->{$key}{$option_key} ); |
|
0
|
|
|
|
|
0
|
|
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
}else{ |
123
|
32
|
|
|
|
|
64
|
$options->{$option_key} = $ini->{$key}{$option_key}; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
8
|
|
|
|
|
14
|
my $subnet; |
128
|
8
|
|
|
|
|
13
|
eval{ |
129
|
8
|
|
|
|
|
45
|
$subnet = Net::DHCP::Config::Utilities::Subnet->new( $options ); |
130
|
|
|
|
|
|
|
}; |
131
|
8
|
50
|
|
|
|
24
|
if ( $@ ){ |
132
|
0
|
|
|
|
|
0
|
die('Failed to create a subnet for the section "'.$key.'"... '.$@); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
8
|
|
|
|
|
12
|
eval{ |
136
|
8
|
|
|
|
|
41
|
$self->{obj}->subnet_add( $subnet ); |
137
|
|
|
|
|
|
|
}; |
138
|
8
|
100
|
|
|
|
33
|
if ( $@ ){ |
139
|
1
|
|
|
|
|
41
|
die( 'Failed to add the subnet in the section "'.$key.'"... '.$@ ); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
7
|
|
|
|
|
44
|
return 1; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 load_dir |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
This loads the specified directory. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Two arguments are taken. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
The first and required is the directory to load. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
The second and optional is the name glob to use. If none |
156
|
|
|
|
|
|
|
is specified then '*.dhcp.ini' is used. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Upon error, this will die. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
my $loaded; |
161
|
|
|
|
|
|
|
eval{ |
162
|
|
|
|
|
|
|
$loaded = $load_dir->load_dir{ $dir }; |
163
|
|
|
|
|
|
|
}; |
164
|
|
|
|
|
|
|
if( $@ ){ |
165
|
|
|
|
|
|
|
die( 'Failed to load... '.$@ ); |
166
|
|
|
|
|
|
|
}else{ |
167
|
|
|
|
|
|
|
print "Loaded ".$loaded." files."; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub load_dir{ |
173
|
2
|
|
|
2
|
1
|
20
|
my $self=$_[0]; |
174
|
2
|
|
|
|
|
12
|
my $dir=$_[1]; |
175
|
2
|
|
|
|
|
11
|
my $name=$_[2]; |
176
|
|
|
|
|
|
|
|
177
|
2
|
50
|
|
|
|
59
|
if ( ! defined( $dir ) ){ |
|
|
50
|
|
|
|
|
|
178
|
0
|
|
|
|
|
0
|
die( 'No directory specified' ); |
179
|
|
|
|
|
|
|
}elsif( ! -d $dir ){ |
180
|
0
|
|
|
|
|
0
|
die( '"'.$dir.'" is not a directory or does not exist' ); |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
2
|
50
|
|
|
|
13
|
if ( !defined( $name ) ){ |
184
|
2
|
|
|
|
|
9
|
$name='*.dhcp.ini'; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
2
|
|
|
|
|
157
|
my @files=File::Find::Rule->file() |
188
|
|
|
|
|
|
|
->name( $name ) |
189
|
|
|
|
|
|
|
->in( $dir ); |
190
|
|
|
|
|
|
|
|
191
|
2
|
50
|
|
|
|
2558
|
if( ! defined( $files[0] ) ){ |
192
|
0
|
|
|
|
|
0
|
return 0; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
2
|
|
|
|
|
6
|
my $count=1; |
196
|
2
|
|
|
|
|
6
|
my $total=$#files + 1; |
197
|
2
|
|
|
|
|
8
|
foreach my $file ( @files ){ |
198
|
4
|
|
|
|
|
9
|
eval{ |
199
|
4
|
|
|
|
|
14
|
$self->load_file( $file ); |
200
|
|
|
|
|
|
|
}; |
201
|
4
|
50
|
|
|
|
15
|
if ( $@ ){ |
202
|
0
|
|
|
|
|
0
|
die( 'Failed on "'.$file.'", '.$count.' of '.$total.'... '.$@ ); |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
4
|
|
|
|
|
8
|
$count++; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
2
|
|
|
|
|
9
|
return $total; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 INI EXPECTATIONS |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Each sesction of a INI file is treated as its own subnet. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
The variable/values taken must be understood by L. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
If the variable base is not specified, the section name is used. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Any variable matching /^range/ is added to the ranges array used when creating the subnet. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
[10.0.0.0] |
222
|
|
|
|
|
|
|
mask=255.255.0.0 |
223
|
|
|
|
|
|
|
dns=10.0.0.1 , 10.0.10.1 |
224
|
|
|
|
|
|
|
desc=a /16 |
225
|
|
|
|
|
|
|
routers=10.0.0.1 |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
[foo] |
228
|
|
|
|
|
|
|
base=192.168.0.0 |
229
|
|
|
|
|
|
|
mask=255.255.0.0 |
230
|
|
|
|
|
|
|
dns=10.0.0.1 , 10.0.10.1 |
231
|
|
|
|
|
|
|
routers=192.168.0.1 |
232
|
|
|
|
|
|
|
range=192.168.0.100 192.168.0.200 |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 AUTHOR |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Zane C. Bowers-Hadley, C<< >> |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 BUGS |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
241
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
242
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head1 SUPPORT |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
perldoc Net::DHCP::Config::Utilities |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
You can also look for information at: |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=over 4 |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
L |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
L |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=item * CPAN Ratings |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
L |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=item * Search CPAN |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
L |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=back |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
This software is Copyright (c) 2019 by Zane C. Bowers-Hadley. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
This is free software, licensed under: |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=cut |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
1; # End of Net::DHCP::Config::Utilities |