blib/lib/Net/Kalk.pm untested

filter: Statementstmt 0.0% 0 / 338 Branchbran 0.0% 0 / 198 Conditioncond 0.0% 0 / 63 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 660 CC 100 SCAR 45.4
CC
100
Cov
0%
error
306.0
range
156.0
ip
110.0
CRAP 93.7
1 # version : 2.06 - November 2020
2 # author : Thierry LE GALL
3 # contact : facila@gmx.fr
4  
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17  
18 package Net::Kalk;
19  
20 use 5.006;
21 use strict;
22 use warnings;
23  
24 our $VERSION = '2.06';
25  
26 my @mask = qw ( 0.0.0.0
27 128.0.0.0 192.0.0.0 224.0.0.0 240.0.0.0 248.0.0.0 252.0.0.0 254.0.0.0 255.0.0.0
28 255.128.0.0 255.192.0.0 255.224.0.0 255.240.0.0 255.248.0.0 255.252.0.0 255.254.0.0 255.255.0.0
29 255.255.128.0 255.255.192.0 255.255.224.0 255.255.240.0 255.255.248.0 255.255.252.0 255.255.254.0 255.255.255.0
30 255.255.255.128 255.255.255.192 255.255.255.224 255.255.255.240 255.255.255.248 255.255.255.252 255.255.255.254 255.255.255.255 );
31  
32 my $r_format = "%-15s %-15s %-15s %3s %-15s %10s\n";
33 my $s_format = " %s%-3s %-15s %-15s %10s %10s\n";
34  
35 sub error {
36 return 1 if $#_ != 1;
37 my ($fct,$x) = @_;
38 return 1 if ! defined($x);
39  
40 if ($fct eq 'ip' ) { return 0 if $x =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ && $1 < 256 && $2 < 256 && $3 < 256 && $4 < 256 }
41 elsif ($fct eq 'mask' ) { foreach(0..32) { return 0 if $mask[$_] eq $x } }
42 elsif ($fct eq 'cidr' ) { return 0 if $x !~ /\D/ && $x < 33 }
43 elsif ($fct eq 'x12' ) { return 0 if $x =~ /^(\d{3})(\d{3})(\d{3})(\d{3})$/ && $1 < 256 && $2 < 256 && $3 < 256 && $4 < 256 }
44 elsif ($fct eq 'decimal') { return 0 if $x !~ /\D/ && $x < 4294967296 }
45 elsif ($fct eq 'hexa' ) { return 0 if $x =~ /^[[:xdigit:]]{2}\.[[:xdigit:]]{2}\.[[:xdigit:]]{2}\.[[:xdigit:]]{2}$/ }
46 elsif ($fct eq 'binary' ) { return 0 if $x =~ /^[01]{8}\.[01]{8}\.[01]{8}\.[01]{8}$/ }
47  
48 return 1 }
49  
50 sub plus { &local_oper('plus' ,@_) }
51 sub minus { &local_oper('minus',@_) }
52  
53 sub local_oper {
54 return if $#_ != 2;
55 my ($fct,$x,$ip) = @_;
56 return if $x =~ /\D/;
57 return if &error('ip',$ip);
58  
59 $ip = &decimal('ip',$ip);
60 if ($fct eq 'plus' ) { $ip += $x }
61 elsif ($fct eq 'minus') { $ip -= $x }
62 $ip = &ip('decimal',$ip);
63 return $ip if ! &error('ip',$ip) }
64  
65 sub invers {
66 return if $#_ != 0;
67 my ($ip) = @_;
68 return if &error('ip',$ip);
69  
70 my ($a,$b,$c,$d) = split /\./,$ip;
71 $a=255-$a; $b=255-$b; $c=255-$c; $d=255-$d;
72 return "$a.$b.$c.$d";
73 }
74  
75 sub mask {
76 return if $#_ != 0;
77 my ($x) = @_;
78 foreach(0..32) {
79 return $mask[$_] if $x eq $_;
80 return $_ if $x eq $mask[$_] } }
81  
82 sub x12 { &local_dst_no_ip('x12' ,@_) }
83 sub decimal { &local_dst_no_ip('decimal',@_) }
84 sub hexa { &local_dst_no_ip('hexa' ,@_) }
85 sub binary { &local_dst_no_ip('binary' ,@_) }
86  
87 sub local_dst_no_ip {
88 return if $#_ != 2;
89 my ($dst,$src,$ip) = @_;
90  
91 $ip = &ip($src,$ip) if $src ne 'ip' ;
92 return if &error('ip',$ip);
93  
94 my ($a,$b,$c,$d) = split /\./,$ip;
95 if ( $dst eq 'hexa' ) { return sprintf "%02X.%02X.%02X.%02X" ,$a,$b,$c,$d }
96 elsif ( $dst eq 'binary' ) { return sprintf "%.8b.%.8b.%.8b.%.8b" ,$a,$b,$c,$d }
97 elsif ( $dst eq 'x12' ) { return sprintf "%03d%03d%03d%03d" ,$a,$b,$c,$d }
98 elsif ( $dst eq 'decimal' ) { return $a*2**24 + $b*2**16 + $c*2**8 + $d } }
99  
100 sub ip {
101 return if $#_ != 1;
102 my ($src,$add) = @_;
103  
104 return if $src eq 'ip';
105 return if &error($src,$add);
106  
107 my ($ip,$a,$b,$c,$d);
108 ($a,$b,$c,$d) = split /\./,$add if $src =~ /hexa|binary/;
109 if ( $src eq 'x12' ) { $ip = sprintf "%d.%d.%d.%d" ,$1,$2,$3,$4 if $add =~ /^(\d\d\d)(\d\d\d)(\d\d\d)(\d\d\d)$/ }
110 elsif ( $src eq 'hexa' ) { $ip = sprintf "%d.%d.%d.%d" ,oct("0x$a"),oct("0x$b"),oct("0x$c"),oct("0x$d") }
111 elsif ( $src eq 'binary' ) { $ip = sprintf "%d.%d.%d.%d" ,oct("0b$a"),oct("0b$b"),oct("0b$c"),oct("0b$d") }
112 elsif ( $src eq 'decimal' ) {
113 my($a1,$b1,$c1);
114 $a1 = $add;
115 $a = int($a1/2**24) ; $b1 = $a1%2**24;
116 $b = int($b1/2**16) ; $c1 = $b1%2**16;
117 $c = int($c1/2** 8) ; $d = $c1%2** 8;
118 $ip = "$a.$b.$c.$d" }
119  
120 return $ip }
121  
122 sub network { &local_net('network' ,@_) }
123 sub broadcast { &local_net('broadcast',@_) }
124 sub nb_add { &local_net('nb_add' ,@_) }
125 sub nb_net { &local_net('nb_net' ,@_) }
126 sub net_all { &local_net('net_all' ,@_) }
127  
128 sub local_net {
129 return if $#_ != 2;
130 my ($fct,$ip,$mask) = @_;
131  
132 return if &error('ip' ,$ip );
133 return if &error('mask',$mask);
134  
135 my ($a1,$a2,$a3,$a4) = split /\./,$ip;
136 my ($m1,$m2,$m3,$m4) = split /\./,$mask;
137  
138 my $network = sprintf "%d.%d.%d.%d" , int $m1 & $a1 , int $m2 & $a2 , int $m3 & $a3 , int $m4 & $a4;
139 my $broadcast = sprintf "%d.%d.%d.%d" , 255^$m1 | $a1 , 255^$m2 | $a2 , 255^$m3 | $a3 , 255^$m4 | $a4;
140  
141 my $nb_add = my $nb_net = 0;
142 if ( $fct =~ /all|nb/ ) {
143 $nb_add = &decimal('ip',$broadcast) - &decimal('ip',$network) + 1;
144 $nb_net = 256**4 / $nb_add }
145  
146 if ( $fct eq 'network' ) { return $network }
147 elsif ( $fct eq 'broadcast' ) { return $broadcast }
148 elsif ( $fct eq 'nb_add' ) { return $nb_add }
149 elsif ( $fct eq 'nb_net' ) { return $nb_net }
150 elsif ( $fct eq 'net_all' ) { return "$network $broadcast $nb_add $nb_net" } }
151  
152 sub range {
153 return if $#_ != 1;
154 my ($start,$end) = @_;
155 return if &error('ip',$start);
156 return if &error('ip',$end );
157  
158 my $tmp;
159  
160 # particular case
161 if ( $start eq '0.0.0.0' && $end eq '255.255.255.255' ) {
162 return sprintf $r_format , $start , $end , $start , '/0' , $start , &local_net('nb_add',$start,$start) }
163 elsif ( $start eq $end ) {
164 return sprintf $r_format , $start , $end , '255.255.255.255' , '/32' , '0.0.0.0' , '1' }
165  
166 my $x12_start = &x12('ip',$start);
167 my $x12_end = &x12('ip',$end ); # x12_ AAABBBCCCDDD format for comparisons
168 if ( $x12_start > $x12_end ) {
169 $tmp = $start ; $start = $end ; $end = $tmp;
170 $tmp = $x12_start ; $x12_start = $x12_end ; $x12_end = $tmp }
171  
172 my($network,$broadcast,$x12_network,$x12_broadcast) = '';
173 my($nb_add,$mask,$wildcard,$result,$m,$ok,$n) = '';
174  
175 while (1) {
176 # if start = end or start is odd, the address is inevitably in /32
177 if ( $start eq $end || $x12_start % 2 ) { $ok = 32 }
178 else {
179 # otherwise the value of the mask is varied by dicothomy
180 # - 5 tests to be done by sub-network in all cases to arrive at the final result
181 # - the last included network found will be the result, m between 1 and 31
182 $m = 16; # value of the mask for the first search, format /mask
183 $ok = 0;
184 for $n (3,2,1,0,9) {
185 # values to be tested for $m
186 ($network,$broadcast) = (split / /,&local_net('net_all',$start,&mask($m)))[0,1];
187 $x12_network = &x12('ip' ,$network );
188 $x12_broadcast = &x12('ip' ,$broadcast);
189  
190 # if the tested subnet is included in the range
191 $ok = $m if $x12_network >= $x12_start && $x12_broadcast <= $x12_end;
192  
193 if ( $n != 9 ) {
194 # calculation of the following mask by dichotomy
195 if ( $ok == $m ) { $m = $m-2 ** $n }
196 else { $m = $m+2 ** $n } } } }
197  
198 # result found for 1 subnet with m = ok
199 $mask = &mask($ok);
200 $wildcard = &invers($mask);
201  
202 ($broadcast,$nb_add) = (split / /,&local_net('net_all',$start,$mask))[1,2];
203  
204 $result .= sprintf $r_format , $start , $broadcast , $mask , "/$ok" , $wildcard , $nb_add;
205 return $result if $broadcast eq $end; # last subnet possible so end
206  
207 # reinitialization for the next search from broadcast + 1
208 $start = &plus('1',$broadcast);
209 $x12_start = &x12 ('ip',$start) } }
210  
211 sub sort {
212 return if ! ($#_ == 0 || $#_ == 1);
213 my ($list,$options) = @_;
214 my ($column,$reverse,$unique) = (1,0,0);
215  
216 $options = 1 if ! $options;
217 $column = $1 if $options =~ /(\d+)/;
218 $reverse = 1 if $options =~ /r/;
219 $unique = 1 if $options =~ /u/;
220 $column--;
221  
222 my $result;
223 my @list;
224 my %unique;
225 my %x12;
226 my $ip;
227 my $i=0;
228  
229 foreach(split /;|\n/,$list) {
230 next if $unique && $unique{$_};
231 push @list,$_;
232 $unique{$_} = 1;
233 $ip = (split /\s+/)[$column];
234 return if &error('ip',$ip);
235 $x12{$i} = sprintf "%03d%03d%03d%03d", split /\./ , $ip;
236 $i++ }
237  
238 foreach ( sort { if ($reverse) { $x12{$b}<=>$x12{$a} } else { $x12{$a}<=>$x12{$b} } } keys %x12 ) {
239 $result .= "$list[$_]\n" }
240 return $result }
241  
242 sub summary {
243 return if ! ($#_ == 0 || $#_ == 1);
244 my $list = $_[0];
245 my $detail = $_[1] if $_[1];
246  
247 my @list_1;
248 my(%list_2,%list_3,%list_4);
249 my($result,$text_1,$text_2,$text_3,$text_4,$text_5);
250  
251 return if &local_list_1(\$list ,\@list_1,\$text_1) == 99 ; # 1 : List of networks to summarize
252 &local_list_2(\@list_1,\%list_2,\$text_2); # 2 : List format start end : ip + decimal
253 &local_list_3(\%list_2,\%list_3,\$text_3); # 3 : List after deleting included ranges and sorting
254 &local_list_4(\%list_3,\%list_4,\$text_4); # 4 : List after grouping ranges that follow or overlap
255 &local_list_5(\%list_4,\$result,\$text_5); # 5 : Summary
256  
257 $result .= "\n$text_1\n$text_2\n$text_3\n$text_4\n$text_5" if $detail && $detail eq 'd';
258 return $result }
259  
260 sub included {
261 return if ! ($#_ == 0 || $#_ == 1);
262 my $list = $_[0];
263 my $detail = $_[1] if $_[1];
264  
265 my @list_1;
266 my %list_2;
267 my($start,$end,$result,$text_1,$text_2,$text_3,$text_4);
268  
269 return if &local_list_1 (\$list ,\@list_1,\$text_1) == 99 ; # 1 : List of networks to summarize
270 &local_list_2 (\@list_1,\%list_2,\$text_2); # 2 : List format start end : ip + decimal
271 &local_range_1(\%list_2,\$start,\$end,\$text_3); # 3 : Range start end to include
272 &local_range_2(\$start,\$end,\$result,\$text_4); # 4 : Network that includes the range
273  
274 $result .= "\n$text_1\n$text_2\n$text_3\n$text_4" if $detail && $detail eq 'd';
275 return $result }
276  
277 sub local_list_1 {
278 # List of networks to summarize
279 my($ref_in,$ref_out,$ref_text) = @_;
280 my($i,$ip,$mask);
281 $i = 0;
282 foreach ( split/;|\n/,$$ref_in ) {
283 $ip = $mask = '';
284 if ( /(.+)\/(.+)/ ) { $ip = $1 ; $mask = "/$2"; return 99 if &error('cidr',$2) }
285 elsif ( /(.+) (.+)/ ) { $ip = $1 ; $mask = $2 }
286 elsif ( /(.+)/ ) { $ip = $1 ; $mask = '' }
287 else { next }
288 $ip =~ tr/ //d; return 99 if &error('ip',$ip);
289 $mask =~ tr/ //d;
290 push @$ref_out , "$ip $mask";
291 $$ref_text .= sprintf $s_format , '1.' , ++$i , $ip , $mask , '' , '' }
292 return 99 if $i < 2 }
293  
294 sub local_list_2 {
295 # List format start end : ip + decimal
296 my($ref_in,$ref_out,$ref_text) = @_;
297 my $i = 0;
298 foreach ( @$ref_in ) {
299 my ($ip,$mask) = split;
300 my $invers = &invers($mask);
301  
302 if ( ! $mask ) { $mask = '255.255.255.255' }
303 elsif ( $mask =~ /\/(.+)/ ) { $mask = &mask($1) }
304 elsif ( ! &error('mask',$invers) ) { $mask = $invers }
305 elsif ( &error('mask',$mask ) ) { return }
306  
307 my $start = &local_net('network' ,$ip,$mask);
308 my $end = &local_net('broadcast',$ip,$mask);
309 my $x_start = &decimal('ip',$start);
310 my $x_end = &decimal('ip',$end);
311 $$ref_text .= sprintf $s_format , '2.' , ++$i , $start , $end , $x_start , $x_end;
312  
313 $$ref_out{"$x_start $x_end"} = "$start $end" } }
314  
315 sub local_list_3 {
316 # List after deleting included ranges and sorting
317 my($ref_in,$ref_out,$ref_text) = @_;
318  
319 # remove the included ranges and create a list out on x_start to be able to sort it
320 foreach my $x1 ( keys %$ref_in ) {
321 my ( $x1_start , $x1_end ) = split / / , $x1;
322 my $ok = 1;
323 foreach my $x2 ( keys %$ref_in ) {
324 my ( $x2_start , $x2_end ) = split / / , $x2;
325 if ( $x1_start == $x2_start && $x1_end == $x2_end ) { next }
326 if ( $x1_start >= $x2_start && $x1_end <= $x2_end ) { $ok = 0; last } }
327 if ( $ok ) { $$ref_out{$x1_start} = "$x1_end $$ref_in{$x1}" } }
328  
329 my $i = 0;
330 foreach my $x_start ( sort { $a <=> $b } keys %$ref_out ) {
331 my ( $x_end , $start , $end ) = split / / , $$ref_out{$x_start};
332 $$ref_text .= sprintf $s_format , '3.' , ++$i , $start , $end , $x_start , $x_end } }
333  
334 sub local_list_4 {
335 # List after grouping ranges that follow or overlap
336 my($ref_in,$ref_out,$ref_text) = @_;
337 my($x_start,$x_end,$start,$end);
338 my($new_start , $x_new_start);
339 my($new_end , $x_new_end );
340 my $first = 1;
341 my $i = 0;
342 foreach ( sort { $a <=> $b } keys %$ref_in ) {
343 if ( $first ) {
344 $first = 0;
345 $x_start = $_;
346 ( $x_end , $start , $end ) = split / / , $$ref_in{$_} }
347 else {
348 $x_new_start = $_;
349 ( $x_new_end , $new_start , $new_end ) = split / / , $$ref_in{$_};
350  
351 if ( $x_new_start <= $x_end + 1 ) {
352 $x_end = $x_new_end;
353 $end = $new_end }
354 else {
355 $$ref_out{$x_start} = "$start;$end";
356 $$ref_text .= sprintf $s_format , '4.' , ++$i , $start , $end , $x_start , $x_end;
357  
358 $x_start = $x_new_start;
359 $x_end = $x_new_end ;
360 $start = $new_start ;
361 $end = $new_end } } }
362  
363 $$ref_out{$x_start} = "$start;$end";
364 $$ref_text .= sprintf $s_format , '4.' , ++$i , $start , $end , $x_start , $x_end }
365  
366 sub local_list_5 {
367 # Summary
368 my($ref_in,$ref_result,$ref_text) = @_;
369 my $i = 0;
370 foreach ( sort { $a <=> $b } keys %$ref_in ) {
371 my ( $start , $end ) = split /;/,$$ref_in{$_};
372 foreach ( split /\n/,&range($start,$end) ) {
373 $$ref_result .= "$_\n";
374 ( $start , $end ) = (split /\s+/)[0,1];
375 my $x_start = &decimal('ip',$start);
376 my $x_end = &decimal('ip',$end);
377 $$ref_text .= sprintf $s_format , '5.' , ++$i , $start , $end , $x_start , $x_end } } }
378  
379 sub local_range_1 {
380 # Range start end to include
381 my($ref_list,$ref_start,$ref_end,$ref_text) = @_;
382  
383 my($start,$end,$x_start,$x_end);
384 my($min ,$max,$x_min ,$x_max);
385 $x_min = -1;
386  
387 foreach ( keys %$ref_list ) {
388 ( $x_start , $x_end ) = split / /;
389 ( $start , $end ) = split / / , $$ref_list{$_};
390 if ( $x_min == -1 ) {
391 $min = $start;
392 $max = $end;
393 $x_min = $x_start;
394 $x_max = $x_end }
395 else {
396 if ( $x_start < $x_min ) { $min = $start ; $x_min = $x_start }
397 if ( $x_end > $x_max ) { $max = $end ; $x_max = $x_end } } }
398  
399 $$ref_start = $min;
400 $$ref_end = $max;
401 $$ref_text = sprintf $s_format , '3.' , '1' , $min , $max , $x_min , $x_max }
402  
403 sub local_range_2 {
404 # Network that includes the range
405 my($ref_start,$ref_end,$ref_result,$ref_text) = @_;
406  
407 my($cidr,$mask,$broadcast,$decimal_broadcast,$decimal_end);
408 my($start,$end,$nb_add,$x_start,$x_end);
409  
410 if ( $$ref_start eq $$ref_end ) {
411 $cidr = 32 }
412 else {
413 $decimal_end = &decimal('ip',$$ref_end);
414  
415 for(1..32) {
416 $cidr = $_;
417 $mask = &mask($cidr);
418 $broadcast = &local_net('broadcast',$$ref_start,$mask);
419 $decimal_broadcast = &decimal('ip',$broadcast);
420 last if $decimal_broadcast < $decimal_end }
421  
422 # included ok for the last but one value of $cidr
423 $cidr-- }
424  
425 $mask = &mask($cidr);
426 ($start,$end,$nb_add) = (split / /,&net_all($$ref_start,$mask))[0,1,2];
427 $$ref_result = sprintf $r_format , $start , $end , $mask , "/$cidr" , &invers($mask) , $nb_add;
428  
429 $x_start = &decimal('ip',$start);
430 $x_end = &decimal('ip',$end);
431 $$ref_text = sprintf $s_format , '4.' , '1' , $start , $end , $x_start , $x_end }
432  
433 1;
434  
435 __END__