| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
3
|
|
|
3
|
|
93801
|
use strict; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
125
|
|
|
2
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
160
|
|
|
3
|
|
|
|
|
|
|
package Net::IPAddress::Minimal; |
|
4
|
|
|
|
|
|
|
BEGIN { |
|
5
|
3
|
|
|
3
|
|
61
|
$Net::IPAddress::Minimal::VERSION = '0.05'; |
|
6
|
|
|
|
|
|
|
} |
|
7
|
|
|
|
|
|
|
# ABSTRACT: IP string to number and back |
|
8
|
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
4845
|
use Data::Validate 'is_integer'; |
|
|
3
|
|
|
|
|
320238
|
|
|
|
3
|
|
|
|
|
296
|
|
|
10
|
3
|
|
|
3
|
|
3580
|
use Data::Validate::IP 'is_ipv4'; |
|
|
3
|
|
|
|
|
231411
|
|
|
|
3
|
|
|
|
|
330
|
|
|
11
|
3
|
|
|
3
|
|
28
|
use base 'Exporter'; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
2258
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw( ip_to_num num_to_ip invert_ip ); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub test_string_structure { |
|
16
|
6
|
|
100
|
6
|
1
|
26
|
my $string = shift || q{}; |
|
17
|
|
|
|
|
|
|
|
|
18
|
6
|
100
|
|
|
|
30
|
is_ipv4($string) && return 'ip'; |
|
19
|
4
|
100
|
|
|
|
127
|
is_integer($string) && return 'num'; |
|
20
|
2
|
100
|
|
|
|
132
|
$string || return 'empty'; |
|
21
|
|
|
|
|
|
|
|
|
22
|
1
|
|
|
|
|
4
|
return 'err'; |
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub ip_to_num { |
|
26
|
|
|
|
|
|
|
# Converting between IP to number is according to this formula: |
|
27
|
|
|
|
|
|
|
# IP = A.B.C.D |
|
28
|
|
|
|
|
|
|
# IP Number = A x (256**3) + B x (256**2) + C x 256 + D |
|
29
|
2
|
|
|
2
|
1
|
4
|
my $ip = shift; |
|
30
|
|
|
|
|
|
|
|
|
31
|
2
|
|
|
|
|
10
|
my ( $Aclass, $Bclass, $Cclass, $Dclass ) = split /\./, $ip; |
|
32
|
|
|
|
|
|
|
|
|
33
|
2
|
|
|
|
|
10
|
my $num = ( |
|
34
|
|
|
|
|
|
|
$Aclass * 256**3 + |
|
35
|
|
|
|
|
|
|
$Bclass * 256**2 + |
|
36
|
|
|
|
|
|
|
$Cclass * 256 + |
|
37
|
|
|
|
|
|
|
$Dclass |
|
38
|
|
|
|
|
|
|
); |
|
39
|
|
|
|
|
|
|
|
|
40
|
2
|
|
|
|
|
19
|
return $num; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub num_to_ip { |
|
44
|
2
|
|
|
2
|
1
|
4
|
my $ipnum = shift; |
|
45
|
|
|
|
|
|
|
|
|
46
|
2
|
|
|
|
|
6
|
my $z = $ipnum % 256; |
|
47
|
2
|
|
|
|
|
4
|
$ipnum >>= 8; |
|
48
|
2
|
|
|
|
|
7
|
my $y = $ipnum % 256; |
|
49
|
2
|
|
|
|
|
4
|
$ipnum >>= 8; |
|
50
|
2
|
|
|
|
|
5
|
my $x = $ipnum % 256; |
|
51
|
2
|
|
|
|
|
3
|
$ipnum >>= 8; |
|
52
|
2
|
|
|
|
|
4
|
my $w = $ipnum % 256; |
|
53
|
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
10
|
my $ipstr = "$w.$x.$y.$z"; |
|
55
|
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
20
|
return $ipstr; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub invert_ip { |
|
60
|
7
|
|
|
7
|
1
|
2939
|
my $input_str = shift; |
|
61
|
7
|
|
|
|
|
26
|
my $result = test_string_structure($input_str); |
|
62
|
|
|
|
|
|
|
my %responses = ( |
|
63
|
2
|
|
|
2
|
|
22
|
ip => sub { ip_to_num($input_str) }, |
|
64
|
2
|
|
|
2
|
|
15
|
num => sub { num_to_ip($input_str) }, |
|
65
|
1
|
|
|
1
|
|
10
|
err => sub { 'Illegal string. Please use IPv4 strings or numbers.' }, |
|
66
|
1
|
|
|
1
|
|
11
|
empty => sub { 'Empty string. Please use IPv4 strings or numbers.' }, |
|
67
|
7
|
|
|
|
|
19310
|
); |
|
68
|
|
|
|
|
|
|
# This is a dispatch table, instead of a big ugly if block / switch |
|
69
|
|
|
|
|
|
|
|
|
70
|
7
|
100
|
|
|
|
44
|
if ( exists $responses{$result} ) { |
|
71
|
6
|
|
|
|
|
21
|
return $responses{$result}->(); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# If none of the above was executed |
|
75
|
1
|
|
|
|
|
21
|
die 'Could not convert IP string / number due to unknown error'; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=pod |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 NAME |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Net::IPAddress::Minimal - IP string to number and back |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 VERSION |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
version 0.05 |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This module converts IPv4 strings to integer IP numbers and vice versa. |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
It's built to be used as quickly and easily as possible, which is why you can |
|
97
|
|
|
|
|
|
|
just simply use the C function. |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
It recognizes whether you have an IPv4 string or a number and converts it to the |
|
100
|
|
|
|
|
|
|
other form. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Here's a sample script: |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
use Net::IPAddress::Minimal ('invert_ip'); |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $input_string = shift @ARGV; |
|
107
|
|
|
|
|
|
|
my $output = invert_ip( $input_string ); |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
print "$output\n"; |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 EXPORT |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Three functions can be exported: |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=over 4 |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item * invert_ip |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item * num_to_ip |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item * ip_to_num |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=back |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 invert_ip |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Gets an IPv4 string or an IP number and converts it to the other form. |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $ip_num = invert_ip( '10.200.10.130' ); |
|
132
|
|
|
|
|
|
|
# $ip_str = 180882050 |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $ip_num = invert_ip( 180882050 ); |
|
135
|
|
|
|
|
|
|
# $ip_str = '10.200.10.130'; |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 num_to_ip |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Gets an IP number and returns an IPv4 string. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $ip_num = num_to_ip( 3232235778 ); |
|
142
|
|
|
|
|
|
|
# $ip_str = '192.168.1.2'; |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 ip_to_num |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Gets a IPv4 string and returns the matching IP number. |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $ip_num = ip_to_num( '212.212.212.212' ); |
|
149
|
|
|
|
|
|
|
# $ip_num = 3570717908 |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 test_string_structure |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Checks the structure of the input string and returns flags indicating whether |
|
154
|
|
|
|
|
|
|
it's an IPv4 string, and IP number or something else (which is an error). |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 BUGS |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
We encourage you to open bugs on the Github Issues page: |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L. |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 AUTHORS |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Tamir Lousky |
|
165
|
|
|
|
|
|
|
Sawyer X |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Tamir Lousky. |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
172
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
__END__ |