line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::IP::Minimal; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Net::IP::Minimal::VERSION = '0.06'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
#ABSTRACT: Minimal functions from Net::IP |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
22718
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
971
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
require Exporter; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw(ip_get_version ip_is_ipv4 ip_is_ipv6); |
15
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'PROC' => [ @EXPORT_OK ] ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub ip_get_version { |
18
|
2
|
|
|
2
|
1
|
6
|
my $ip = shift; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# If the address does not contain any ':', maybe it's IPv4 |
21
|
2
|
100
|
66
|
|
|
12
|
$ip !~ /:/ and ip_is_ipv4($ip) and return '4'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Is it IPv6 ? |
24
|
1
|
50
|
|
|
|
4
|
ip_is_ipv6($ip) and return '6'; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
0
|
return; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub ip_is_ipv4 { |
30
|
3
|
|
|
3
|
1
|
14
|
my $ip = shift; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Check for invalid chars |
33
|
3
|
50
|
|
|
|
21
|
unless ($ip =~ m/^[\d\.]+$/) { |
34
|
0
|
|
|
|
|
0
|
return 0; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
3
|
50
|
|
|
|
10
|
if ($ip =~ m/^\./) { |
38
|
0
|
|
|
|
|
0
|
return 0; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
3
|
50
|
|
|
|
10
|
if ($ip =~ m/\.$/) { |
42
|
0
|
|
|
|
|
0
|
return 0; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Single Numbers are considered to be IPv4 |
46
|
3
|
50
|
33
|
|
|
18
|
if ($ip =~ m/^(\d+)$/ and $1 < 256) { return 1 } |
|
0
|
|
|
|
|
0
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Count quads |
49
|
3
|
|
|
|
|
7
|
my $n = ($ip =~ tr/\./\./); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# IPv4 must have from 1 to 4 quads |
52
|
3
|
50
|
33
|
|
|
29
|
unless ($n >= 0 and $n < 4) { |
53
|
0
|
|
|
|
|
0
|
return 0; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Check for empty quads |
57
|
3
|
50
|
|
|
|
10
|
if ($ip =~ m/\.\./) { |
58
|
0
|
|
|
|
|
0
|
return 0; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
15
|
foreach (split /\./, $ip) { |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Check for invalid quads |
64
|
12
|
50
|
33
|
|
|
65
|
unless ($_ >= 0 and $_ < 256) { |
65
|
0
|
|
|
|
|
0
|
return 0; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
3
|
|
|
|
|
29
|
return 1; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub ip_is_ipv6 { |
72
|
3
|
|
|
3
|
1
|
657
|
my $ip = shift; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Count octets |
75
|
3
|
|
|
|
|
7
|
my $n = ($ip =~ tr/:/:/); |
76
|
3
|
50
|
33
|
|
|
17
|
return 0 unless ($n > 0 and $n < 8); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# $k is a counter |
79
|
3
|
|
|
|
|
5
|
my $k; |
80
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
15
|
foreach (split /:/, $ip) { |
82
|
20
|
|
|
|
|
22
|
$k++; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Empty octet ? |
85
|
20
|
100
|
|
|
|
36
|
next if ($_ eq ''); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Normal v6 octet ? |
88
|
18
|
100
|
|
|
|
61
|
next if (/^[a-f\d]{1,4}$/i); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Last octet - is it IPv4 ? |
91
|
1
|
50
|
33
|
|
|
9
|
if ( ($k == $n + 1) && ip_is_ipv4($_) ) { |
92
|
1
|
|
|
|
|
2
|
$n++; # ipv4 is two octets |
93
|
1
|
|
|
|
|
2
|
next; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
0
|
return 0; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Does the IP address start with : ? |
100
|
3
|
50
|
|
|
|
17
|
if ($ip =~ m/^:[^:]/) { |
101
|
0
|
|
|
|
|
0
|
return 0; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Does the IP address finish with : ? |
105
|
3
|
50
|
|
|
|
9
|
if ($ip =~ m/[^:]:$/) { |
106
|
0
|
|
|
|
|
0
|
return 0; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Does the IP address have more than one '::' pattern ? |
110
|
3
|
50
|
|
|
|
24
|
if ($ip =~ s/:(?=:)/:/g > 1) { |
111
|
0
|
|
|
|
|
0
|
return 0; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# number of octets |
115
|
3
|
50
|
66
|
|
|
14
|
if ($n != 7 && $ip !~ /::/) { |
116
|
0
|
|
|
|
|
0
|
return 0; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# valid IPv6 address |
120
|
3
|
|
|
|
|
19
|
return 1; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
qq[IP freely]; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__END__ |