line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package IP::Decimal; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
691
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
5
|
1
|
|
|
1
|
|
4
|
use Exporter; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
6
|
1
|
|
|
|
|
81
|
use Data::Validate::IP qw/ |
7
|
|
|
|
|
|
|
is_ipv4 |
8
|
|
|
|
|
|
|
is_ipv6 |
9
|
1
|
|
|
1
|
|
562
|
/; |
|
1
|
|
|
|
|
36666
|
|
10
|
1
|
|
|
|
|
7
|
use NetAddr::IP::Util qw/ |
11
|
|
|
|
|
|
|
inet_aton |
12
|
|
|
|
|
|
|
inet_ntoa |
13
|
|
|
|
|
|
|
ipv6_aton |
14
|
|
|
|
|
|
|
ipv6_ntoa |
15
|
1
|
|
|
1
|
|
11
|
/; |
|
1
|
|
|
|
|
2
|
|
16
|
1
|
|
|
|
|
368
|
use Math::Int128 qw/ |
17
|
|
|
|
|
|
|
net_to_uint128 |
18
|
|
|
|
|
|
|
uint128_to_net |
19
|
1
|
|
|
1
|
|
593
|
/; |
|
1
|
|
|
|
|
5235
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our @EXPORT = qw/ |
22
|
|
|
|
|
|
|
ipv4_to_decimal |
23
|
|
|
|
|
|
|
decimal_to_ipv4 |
24
|
|
|
|
|
|
|
ipv6_to_decimal |
25
|
|
|
|
|
|
|
decimal_to_ipv6 |
26
|
|
|
|
|
|
|
/; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our $VERSION = 0.01; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub new { |
31
|
0
|
|
|
0
|
0
|
0
|
return bless {}, shift; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub ipv4_to_decimal { |
35
|
1
|
|
|
1
|
1
|
554
|
my ($self, $ipv4) = @_; |
36
|
|
|
|
|
|
|
|
37
|
1
|
50
|
|
|
|
4
|
return unpack 'N', inet_aton($ipv4) if is_ipv4 $ipv4; |
38
|
0
|
|
|
|
|
0
|
return undef; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub decimal_to_ipv4 { |
42
|
1
|
|
|
1
|
1
|
616
|
my ($self, $decimal) = @_; |
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
10
|
my $ipv4 = join '.', inet_ntoa(pack 'N', $decimal); |
45
|
1
|
50
|
|
|
|
377
|
return $ipv4 if is_ipv4 $ipv4; |
46
|
0
|
|
|
|
|
0
|
return undef; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub ipv6_to_decimal { |
50
|
1
|
|
|
1
|
1
|
3
|
my ($self, $ipv6) = @_; |
51
|
|
|
|
|
|
|
|
52
|
1
|
50
|
|
|
|
4
|
return net_to_uint128(ipv6_aton($ipv6)) if is_ipv6 $ipv6; |
53
|
0
|
|
|
|
|
0
|
return undef; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub decimal_to_ipv6 { |
57
|
1
|
|
|
1
|
1
|
663
|
my ($self, $decimal) = @_; |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
8
|
my $ipv6 = ipv6_ntoa(uint128_to_net($decimal)); |
60
|
1
|
50
|
|
|
|
1508
|
return $ipv6 if is_ipv6 $ipv6; |
61
|
0
|
|
|
|
|
|
return undef; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=encoding utf8 |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 NAME |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
IP::Decimal - Convert IP to Decimal and Decimal to IP |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use IP::Decimal qw/ |
75
|
|
|
|
|
|
|
ipv4_to_decimal |
76
|
|
|
|
|
|
|
decimal_to_ipv4 |
77
|
|
|
|
|
|
|
ipv6_to_decimal |
78
|
|
|
|
|
|
|
decimal_to_ipv6 |
79
|
|
|
|
|
|
|
/; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $decimal = ipv4_to_decimal('127.0.0.1'); # 2130706433 |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $ipv4 = decimal_to_ipv4('2130706433'); # 127.0.0.1 |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 DESCRIPTION |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
This module provides methods for convert IP to Decimal and Decimal to IP, simply and directly. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 METHODS |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 ipv4_to_decimal |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $decimal = ipv4_to_decimal('127.0.0.1'); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Returns the decimal: 2130706433 |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 decimal_to_ipv4 |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my $ip = decimal_to_ipv4('2130706433'); |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Returns the IP 127.0.0.1 |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 ipv6_to_decimal |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $decimal = ipv6_to_decimal('dead:beef:cafe:babe::f0ad'); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Returns the decimal: 295990755076957304698079185533545803949 |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 decimal_to_ipv6 |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $ip = decimal_to_ipv6('295990755076957304698079185533545803949'); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Returns the IP dead:beef:cafe:babe::f0ad |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 AUTHOR |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Lucas Tiago de Moraes C |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
This software is copyright (c) 2020 by Lucas Tiago de Moraes. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |