line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Checker::IP; |
2
|
|
|
|
|
|
|
# Copyright (c) 2014-2016 Sullivan Beck. All rights reserved. |
3
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify it |
4
|
|
|
|
|
|
|
# under the same terms as Perl itself. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
############################################################################### |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require 5.008; |
9
|
1
|
|
|
1
|
|
11
|
use warnings 'all'; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
127
|
|
10
|
1
|
|
|
1
|
|
12
|
use strict; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
84
|
|
11
|
1
|
|
|
1
|
|
1870
|
use NetAddr::IP::Lite qw(:nofqdn); |
|
1
|
|
|
|
|
57416
|
|
|
1
|
|
|
|
|
8
|
|
12
|
1
|
|
|
1
|
|
1469
|
use NetAddr::IP 4.073; |
|
1
|
|
|
|
|
4097
|
|
|
1
|
|
|
|
|
10
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our($VERSION); |
15
|
|
|
|
|
|
|
$VERSION='1.07'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
############################################################################### |
18
|
|
|
|
|
|
|
############################################################################### |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub check { |
21
|
25
|
|
|
25
|
1
|
61
|
my($obj,$element,$desc,$check_opts) = @_; |
22
|
25
|
|
|
|
|
62
|
my $err = []; |
23
|
25
|
|
|
|
|
45
|
my $warn = []; |
24
|
25
|
|
|
|
|
39
|
my $info = []; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# |
27
|
|
|
|
|
|
|
# Must be a valid IP, |
28
|
|
|
|
|
|
|
# |
29
|
|
|
|
|
|
|
# Must be any of the forms supported by NetAddr::IP. |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
|
32
|
25
|
|
|
|
|
132
|
my $ip_obj = new NetAddr::IP $element; |
33
|
25
|
100
|
|
|
|
4823
|
my $valid = ($ip_obj ? 1 : 0); |
34
|
25
|
|
|
|
|
8612
|
$obj->check_value($check_opts,undef,$element,$valid, |
35
|
|
|
|
|
|
|
"Not a valid IP",undef, |
36
|
|
|
|
|
|
|
$err,$warn,$info); |
37
|
25
|
100
|
|
|
|
117
|
return ($element,$err,$warn,$info) if (@$err); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
# Check IP version. |
41
|
|
|
|
|
|
|
# |
42
|
|
|
|
|
|
|
|
43
|
22
|
|
|
|
|
120
|
my $vers = $ip_obj->version(); |
44
|
22
|
100
|
|
|
|
190
|
if ($obj->check_performed($check_opts,'ipv4')) { |
45
|
4
|
|
|
|
|
7
|
my $is4 = ($vers == 4); |
46
|
4
|
|
|
|
|
18
|
$obj->check_value($check_opts,'ipv4',$element,$is4, |
47
|
|
|
|
|
|
|
"IPv4 IP required", |
48
|
|
|
|
|
|
|
"Non-IPv4 IP required", |
49
|
|
|
|
|
|
|
$err,$warn,$info); |
50
|
4
|
100
|
|
|
|
25
|
return ($element,$err,$warn,$info) if (@$err); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
20
|
100
|
|
|
|
62
|
if ($obj->check_performed($check_opts,'ipv6')) { |
54
|
4
|
|
|
|
|
8
|
my $is6 = ($vers == 6); |
55
|
4
|
|
|
|
|
12
|
$obj->check_value($check_opts,'ipv6',$element,$is6, |
56
|
|
|
|
|
|
|
"IPv6 IP required", |
57
|
|
|
|
|
|
|
"Non-IPv6 IP required", |
58
|
|
|
|
|
|
|
$err,$warn,$info); |
59
|
4
|
100
|
|
|
|
27
|
return ($element,$err,$warn,$info) if (@$err); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# |
63
|
|
|
|
|
|
|
# in_network |
64
|
|
|
|
|
|
|
# |
65
|
|
|
|
|
|
|
|
66
|
18
|
|
|
|
|
30
|
my $net_obj; |
67
|
18
|
100
|
|
|
|
45
|
if ($obj->check_performed($check_opts,'in_network')) { |
68
|
7
|
|
|
|
|
25
|
my $val = $obj->check_option($check_opts,'network',undef,'in_network'); |
69
|
7
|
|
|
|
|
33
|
$net_obj = new NetAddr::IP $val; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Must be a valid IP |
72
|
7
|
100
|
|
|
|
1094
|
my $valid = ($net_obj ? 1 : 0); |
73
|
7
|
|
|
|
|
2097
|
$obj->check_value($check_opts,undef,$element,$valid, |
74
|
|
|
|
|
|
|
"in_network must be a valid IP",undef, |
75
|
|
|
|
|
|
|
$err,$warn,$info); |
76
|
7
|
100
|
|
|
|
30
|
return ($element,$err,$warn,$info) if (@$err); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Must be the same version |
79
|
6
|
|
|
|
|
22
|
my $v = $net_obj->version(); |
80
|
6
|
100
|
|
|
|
47
|
if ($v != $vers) { |
81
|
1
|
|
|
|
|
6
|
$obj->check_value($check_opts,undef,$element,0, |
82
|
|
|
|
|
|
|
"in_network and IP must both be IPv4 or IPv6",undef, |
83
|
|
|
|
|
|
|
$err,$warn,$info); |
84
|
1
|
|
|
|
|
10
|
return ($element,$err,$warn,$info); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Must contain network info |
88
|
5
|
|
|
|
|
21
|
my $mask = $net_obj->masklen(); |
89
|
5
|
100
|
66
|
|
|
140
|
if ( ($vers == 4 && $mask == 32) || |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
90
|
|
|
|
|
|
|
($vers == 6 && $mask == 128) ) { |
91
|
1
|
|
|
|
|
8
|
$obj->check_value($check_opts,undef,$element,0, |
92
|
|
|
|
|
|
|
"in_network must be a valid network IP",undef, |
93
|
|
|
|
|
|
|
$err,$warn,$info); |
94
|
1
|
|
|
|
|
19
|
return ($element,$err,$warn,$info); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
4
|
|
|
|
|
27
|
my $flag = $net_obj->contains($ip_obj); |
98
|
4
|
|
|
|
|
107
|
$obj->check_value($check_opts,'in_network',$element,$flag, |
99
|
|
|
|
|
|
|
"IP not in network", |
100
|
|
|
|
|
|
|
"IP contained in network", |
101
|
|
|
|
|
|
|
$err,$warn,$info); |
102
|
|
|
|
|
|
|
|
103
|
4
|
100
|
|
|
|
35
|
return ($element,$err,$warn,$info) if (@$err); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# |
107
|
|
|
|
|
|
|
# network_ip, broadcast_ip |
108
|
|
|
|
|
|
|
# |
109
|
|
|
|
|
|
|
|
110
|
13
|
100
|
|
|
|
39
|
my $chk_net_ip = ($obj->check_performed($check_opts,'network_ip') ? 1 : 0); |
111
|
13
|
100
|
|
|
|
34
|
my $chk_broad_ip = ($obj->check_performed($check_opts,'broadcast_ip') ? 1 : 0); |
112
|
13
|
100
|
100
|
|
|
125
|
if ( ($chk_net_ip || $chk_broad_ip) && ! $net_obj) { |
|
|
|
66
|
|
|
|
|
113
|
7
|
|
|
|
|
23
|
my $mask = $ip_obj->masklen(); |
114
|
|
|
|
|
|
|
|
115
|
7
|
100
|
66
|
|
|
219
|
if ( ($vers == 4 && $mask == 32) || |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
116
|
|
|
|
|
|
|
($vers == 6 && $mask == 128) ) { |
117
|
1
|
|
|
|
|
8
|
$obj->check_value($check_opts,undef,$element,0, |
118
|
|
|
|
|
|
|
"IP must include network information for " . |
119
|
|
|
|
|
|
|
"network/broadcast check",undef, |
120
|
|
|
|
|
|
|
$err,$warn,$info); |
121
|
1
|
|
|
|
|
10
|
return ($element,$err,$warn,$info); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
6
|
|
|
|
|
17
|
$net_obj = $ip_obj; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
12
|
100
|
|
|
|
36
|
if ($chk_net_ip) { |
128
|
3
|
|
|
|
|
23
|
my $net_ip = $net_obj->network->addr(); |
129
|
3
|
|
|
|
|
602
|
my $ip = $ip_obj->addr(); |
130
|
3
|
|
|
|
|
597
|
$valid = ($ip eq $net_ip); |
131
|
3
|
|
|
|
|
19
|
$obj->check_value($check_opts,'network_ip',$element,$valid, |
132
|
|
|
|
|
|
|
"Network IP required", |
133
|
|
|
|
|
|
|
"Non-network IP required", |
134
|
|
|
|
|
|
|
$err,$warn,$info); |
135
|
3
|
100
|
|
|
|
38
|
return ($element,$err,$warn,$info) if (@$err); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
10
|
100
|
|
|
|
30
|
if ($chk_broad_ip) { |
139
|
3
|
|
|
|
|
31
|
my $broad_ip = $net_obj->broadcast->addr(); |
140
|
3
|
|
|
|
|
864
|
my $ip = $ip_obj->addr(); |
141
|
3
|
|
|
|
|
585
|
$valid = ($ip eq $broad_ip); |
142
|
3
|
|
|
|
|
19
|
$obj->check_value($check_opts,'broadcast_ip',$element,$valid, |
143
|
|
|
|
|
|
|
"Broadcast IP required", |
144
|
|
|
|
|
|
|
"Non-broadcast IP required", |
145
|
|
|
|
|
|
|
$err,$warn,$info); |
146
|
3
|
100
|
|
|
|
27
|
return ($element,$err,$warn,$info) if (@$err); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
8
|
|
|
|
|
96
|
return ($element,$err,$warn,$info); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
1; |
153
|
|
|
|
|
|
|
# Local Variables: |
154
|
|
|
|
|
|
|
# mode: cperl |
155
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
156
|
|
|
|
|
|
|
# cperl-indent-level: 3 |
157
|
|
|
|
|
|
|
# cperl-continued-statement-offset: 2 |
158
|
|
|
|
|
|
|
# cperl-continued-brace-offset: 0 |
159
|
|
|
|
|
|
|
# cperl-brace-offset: 0 |
160
|
|
|
|
|
|
|
# cperl-brace-imaginary-offset: 0 |
161
|
|
|
|
|
|
|
# cperl-label-offset: 0 |
162
|
|
|
|
|
|
|
# End: |