Filename | /usr/local/lib/perl5/site_perl/mach/5.24/NetAddr/IP/Lite.pm |
Statements | Executed 918 statements in 21.9ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 11.9ms | 27.0ms | BEGIN@9 | NetAddr::IP::Lite::
19 | 2 | 1 | 3.45ms | 9.22ms | _xnew | NetAddr::IP::Lite::
1 | 1 | 1 | 2.95ms | 13.9ms | BEGIN@18 | NetAddr::IP::Lite::
117 | 7 | 1 | 520µs | 520µs | CORE:match (opcode) | NetAddr::IP::Lite::
1 | 1 | 1 | 214µs | 832µs | import | NetAddr::IP::Lite::
6 | 1 | 1 | 208µs | 344µs | network | NetAddr::IP::Lite::
6 | 1 | 1 | 137µs | 137µs | _new | NetAddr::IP::Lite::
4 | 1 | 1 | 115µs | 140µs | masklen | NetAddr::IP::Lite::
4 | 1 | 1 | 113µs | 1.54ms | cidr | NetAddr::IP::Lite::
10 | 3 | 2 | 101µs | 101µs | new6 | NetAddr::IP::Lite::
2 | 1 | 1 | 89µs | 119µs | within | NetAddr::IP::Lite::
4 | 1 | 1 | 80µs | 1.29ms | addr | NetAddr::IP::Lite::
9 | 6 | 2 | 72µs | 72µs | new | NetAddr::IP::Lite::
4 | 2 | 1 | 72µs | 1.61ms | __ANON__[:238] | NetAddr::IP::Lite::
1 | 1 | 1 | 72µs | 788µs | BEGIN@228 | NetAddr::IP::Lite::
15 | 5 | 1 | 68µs | 68µs | Ones | NetAddr::IP::Lite::
1 | 1 | 1 | 52µs | 243µs | BEGIN@5 | NetAddr::IP::Lite::
9 | 4 | 1 | 42µs | 42µs | Zeros | NetAddr::IP::Lite::
1 | 1 | 1 | 30µs | 36µs | BEGIN@6 | NetAddr::IP::Lite::
1 | 1 | 1 | 26µs | 485µs | BEGIN@33 | NetAddr::IP::Lite::
2 | 1 | 1 | 25µs | 144µs | contains | NetAddr::IP::Lite::
1 | 1 | 1 | 22µs | 41µs | BEGIN@174 | NetAddr::IP::Lite::
2 | 1 | 1 | 20µs | 20µs | plus | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | AUTOLOAD | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | DESTROY | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | V4mask | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | V4net | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | __ANON__[:244] | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | __ANON__[:250] | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | __ANON__[:255] | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | __ANON__[:260] | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | __ANON__[:264] | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | __ANON__[:268] | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | __ANON__[:272] | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | __ANON__[:276] | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _biRef | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _bi_fake | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _bi_stfy | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _fakebi2strg | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _force_bi_emu | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _loadMBI | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _no_octal | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _obits | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | _retMBIstring | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | aton | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | bigint | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | bits | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | broadcast | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | comp_addr_mask | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | copy | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | first | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | is_local | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | is_rfc1918 | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | last | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | mask | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | minus | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | minusminus | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | new6FFFF | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | new_cis | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | new_cis6 | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | new_from_aton | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | new_no | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | nth | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | num | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | numeric | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | plusplus | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | range | NetAddr::IP::Lite::
0 | 0 | 0 | 0s | 0s | version | NetAddr::IP::Lite::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | #!/usr/bin/perl | ||||
2 | |||||
3 | package NetAddr::IP::Lite; | ||||
4 | |||||
5 | 2 | 72µs | 2 | 435µs | # spent 243µs (52+191) within NetAddr::IP::Lite::BEGIN@5 which was called:
# once (52µs+191µs) by NetAddr::IP::BEGIN@8 at line 5 # spent 243µs making 1 call to NetAddr::IP::Lite::BEGIN@5
# spent 191µs making 1 call to Exporter::import |
6 | 2 | 91µs | 2 | 43µs | # spent 36µs (30+7) within NetAddr::IP::Lite::BEGIN@6 which was called:
# once (30µs+7µs) by NetAddr::IP::BEGIN@8 at line 6 # spent 36µs making 1 call to NetAddr::IP::Lite::BEGIN@6
# spent 7µs making 1 call to strict::import |
7 | #use diagnostics; | ||||
8 | #use warnings; | ||||
9 | 1 | 228µs | # spent 27.0ms (11.9+15.1) within NetAddr::IP::Lite::BEGIN@9 which was called:
# once (11.9ms+15.1ms) by NetAddr::IP::BEGIN@8 at line 17 | ||
10 | inet_any2n | ||||
11 | isIPv4 | ||||
12 | inet_n2dx | ||||
13 | inet_aton | ||||
14 | ipv6_aton | ||||
15 | ipv6_n2x | ||||
16 | fillIPv4 | ||||
17 | 1 | 128µs | 2 | 28.0ms | ); # spent 27.0ms making 1 call to NetAddr::IP::Lite::BEGIN@9
# spent 1.01ms making 1 call to NetAddr::IP::InetBase::import |
18 | 1 | 246µs | # spent 13.9ms (2.95+10.9) within NetAddr::IP::Lite::BEGIN@18 which was called:
# once (2.95ms+10.9ms) by NetAddr::IP::BEGIN@8 at line 31 | ||
19 | addconst | ||||
20 | sub128 | ||||
21 | ipv6to4 | ||||
22 | notcontiguous | ||||
23 | shiftleft | ||||
24 | hasbits | ||||
25 | bin2bcd | ||||
26 | bcd2bin | ||||
27 | mask4to6 | ||||
28 | ipv4to6 | ||||
29 | naip_gethostbyname | ||||
30 | havegethostbyname2 | ||||
31 | 1 | 118µs | 2 | 15.1ms | ); # spent 13.9ms making 1 call to NetAddr::IP::Lite::BEGIN@18
# spent 1.23ms making 1 call to NetAddr::IP::Util::import |
32 | |||||
33 | 2 | 411µs | 2 | 945µs | # spent 485µs (26+459) within NetAddr::IP::Lite::BEGIN@33 which was called:
# once (26µs+459µs) by NetAddr::IP::BEGIN@8 at line 33 # spent 485µs making 1 call to NetAddr::IP::Lite::BEGIN@33
# spent 460µs making 1 call to vars::import |
34 | |||||
35 | 3 | 73µs | 1 | 23µs | $VERSION = do { my @r = (q$Revision: 1.56 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # spent 23µs making 1 call to NetAddr::IP::Lite::CORE:match |
36 | |||||
37 | 1 | 5µs | require Exporter; | ||
38 | |||||
39 | 1 | 24µs | @ISA = qw(Exporter); | ||
40 | |||||
41 | 1 | 5µs | @EXPORT_OK = qw(Zeros Zero Ones V4mask V4net); | ||
42 | |||||
43 | # Set to true, to enable recognizing of ipV4 && ipV6 binary notation IP | ||||
44 | # addresses. Thanks to Steve Snodgrass for reporting. This can be done | ||||
45 | # at the time of use-ing the module. See docs for details. | ||||
46 | |||||
47 | 1 | 2µs | $Accept_Binary_IP = 0; | ||
48 | 1 | 2µs | $Old_nth = 0; | ||
49 | 1 | 7µs | *Zero = \&Zeros; | ||
50 | |||||
51 | =pod | ||||
52 | |||||
53 | =encoding UTF-8 | ||||
54 | |||||
55 | =head1 NAME | ||||
56 | |||||
57 | NetAddr::IP::Lite - Manages IPv4 and IPv6 addresses and subnets | ||||
58 | |||||
59 | =head1 SYNOPSIS | ||||
60 | |||||
61 | use NetAddr::IP::Lite qw( | ||||
62 | Zeros | ||||
63 | Ones | ||||
64 | V4mask | ||||
65 | V4net | ||||
66 | :aton DEPRECATED ! | ||||
67 | :old_nth | ||||
68 | :upper | ||||
69 | :lower | ||||
70 | :nofqdn | ||||
71 | ); | ||||
72 | |||||
73 | my $ip = new NetAddr::IP::Lite '127.0.0.1'; | ||||
74 | or if your prefer | ||||
75 | my $ip = NetAddr::IP::Lite->new('127.0.0.1); | ||||
76 | or from a packed IPv4 address | ||||
77 | my $ip = new_from_aton NetAddr::IP::Lite (inet_aton('127.0.0.1')); | ||||
78 | or from an octal filtered IPv4 address | ||||
79 | my $ip = new_no NetAddr::IP::Lite '127.012.0.0'; | ||||
80 | |||||
81 | print "The address is ", $ip->addr, " with mask ", $ip->mask, "\n" ; | ||||
82 | |||||
83 | if ($ip->within(new NetAddr::IP::Lite "127.0.0.0", "255.0.0.0")) { | ||||
84 | print "Is a loopback address\n"; | ||||
85 | } | ||||
86 | |||||
87 | # This prints 127.0.0.1/32 | ||||
88 | print "You can also say $ip...\n"; | ||||
89 | |||||
90 | The following four functions return ipV6 representations of: | ||||
91 | |||||
92 | :: = Zeros(); | ||||
93 | FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF = Ones(); | ||||
94 | FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:: = V4mask(); | ||||
95 | ::FFFF:FFFF = V4net(); | ||||
96 | |||||
97 | Will also return an ipV4 or ipV6 representation of a | ||||
98 | resolvable Fully Qualified Domanin Name (FQDN). | ||||
99 | |||||
100 | =head1 INSTALLATION | ||||
101 | |||||
102 | Un-tar the distribution in an appropriate directory and type: | ||||
103 | |||||
104 | perl Makefile.PL | ||||
105 | make | ||||
106 | make test | ||||
107 | make install | ||||
108 | |||||
109 | B<NetAddr::IP::Lite> depends on B<NetAddr::IP::Util> which installs by default with its primary functions compiled | ||||
110 | using Perl's XS extensions to build a 'C' library. If you do not have a 'C' | ||||
111 | complier available or would like the slower Pure Perl version for some other | ||||
112 | reason, then type: | ||||
113 | |||||
114 | perl Makefile.PL -noxs | ||||
115 | make | ||||
116 | make test | ||||
117 | make install | ||||
118 | |||||
119 | =head1 DESCRIPTION | ||||
120 | |||||
121 | This module provides an object-oriented abstraction on top of IP | ||||
122 | addresses or IP subnets, that allows for easy manipulations. Most of the | ||||
123 | operations of NetAddr::IP are supported. This module will work with older | ||||
124 | versions of Perl and is compatible with Math::BigInt. | ||||
125 | |||||
126 | * By default B<NetAddr::IP> functions and methods return string IPv6 | ||||
127 | addresses in uppercase. To change that to lowercase: | ||||
128 | |||||
129 | NOTE: the AUGUST 2010 RFC5952 states: | ||||
130 | |||||
131 | 4.3. Lowercase | ||||
132 | |||||
133 | The characters "a", "b", "c", "d", "e", and "f" in an IPv6 | ||||
134 | address MUST be represented in lowercase. | ||||
135 | |||||
136 | It is recommended that all NEW applications using NetAddr::IP::Lite be | ||||
137 | invoked as shown on the next line. | ||||
138 | |||||
139 | use NetAddr::IP::Lite qw(:lower); | ||||
140 | |||||
141 | * To ensure the current IPv6 string case behavior even if the default changes: | ||||
142 | |||||
143 | use NetAddr::IP::Lite qw(:upper); | ||||
144 | |||||
145 | |||||
146 | The internal representation of all IP objects is in 128 bit IPv6 notation. | ||||
147 | IPv4 and IPv6 objects may be freely mixed. | ||||
148 | |||||
149 | The supported operations are described below: | ||||
150 | |||||
151 | =cut | ||||
152 | |||||
153 | # in the off chance that NetAddr::IP::Lite objects are created | ||||
154 | # and the caller later loads NetAddr::IP and expects to use | ||||
155 | # those objects, let the AUTOLOAD routine find and redirect | ||||
156 | # NetAddr::IP::Lite method and subroutine calls to NetAddr::IP. | ||||
157 | # | ||||
158 | |||||
159 | 1 | 2µs | my $parent = 'NetAddr::IP'; | ||
160 | |||||
161 | # test function | ||||
162 | # | ||||
163 | # input: subroutine name in NetAddr::IP | ||||
164 | # output: t/f if sub name exists in NetAddr::IP namespace | ||||
165 | # | ||||
166 | #sub sub_exists { | ||||
167 | # my $other = $parent .'::'; | ||||
168 | # return exists ${$other}{$_[0]}; | ||||
169 | #} | ||||
170 | |||||
171 | sub DESTROY {}; | ||||
172 | |||||
173 | sub AUTOLOAD { | ||||
174 | 2 | 1.49ms | 2 | 59µs | # spent 41µs (22+18) within NetAddr::IP::Lite::BEGIN@174 which was called:
# once (22µs+18µs) by NetAddr::IP::BEGIN@8 at line 174 # spent 41µs making 1 call to NetAddr::IP::Lite::BEGIN@174
# spent 18µs making 1 call to strict::unimport |
175 | my ($pkg,$func) = ($AUTOLOAD =~ /(.*)::([^:]+)$/); | ||||
176 | my $other = $parent .'::'; | ||||
177 | |||||
178 | if ($pkg =~ /^$other/o && exists ${$other}{$func}) { | ||||
179 | $other .= $func; | ||||
180 | goto &{$other}; | ||||
181 | } | ||||
182 | |||||
183 | my @stack = caller(0); | ||||
184 | |||||
185 | if ( $pkg eq ref $_[0] ) { | ||||
186 | $other = qq|Can't locate object method "$func" via|; | ||||
187 | } | ||||
188 | else { | ||||
189 | $other = qq|Undefined subroutine \&$AUTOLOAD not found in|; | ||||
190 | } | ||||
191 | die $other . qq| package "$parent" or "$pkg" (did you forgot to load a module?) at $stack[1] line $stack[2].\n|; | ||||
192 | } | ||||
193 | |||||
194 | =head2 Overloaded Operators | ||||
195 | |||||
196 | =cut | ||||
197 | |||||
198 | # these really should be packed in Network Long order but since they are | ||||
199 | # symmetrical, that extra internal processing can be skipped | ||||
200 | |||||
201 | 1 | 2µs | 1 | 5µs | my $_v4zero = pack('L',0); # spent 5µs making 1 call to main::CORE:pack |
202 | 1 | 2µs | 1 | 10µs | my $_zero = pack('L4',0,0,0,0); # spent 10µs making 1 call to main::CORE:pack |
203 | 1 | 3µs | my $_ones = ~$_zero; | ||
204 | 1 | 2µs | 1 | 4µs | my $_v4mask = pack('L4',0xffffffff,0xffffffff,0xffffffff,0); # spent 4µs making 1 call to main::CORE:pack |
205 | 1 | 2µs | my $_v4net = ~ $_v4mask; | ||
206 | 1 | 2µs | 1 | 8µs | my $_ipv4FFFF = pack('N4',0,0,0xffff,0); # spent 8µs making 1 call to main::CORE:pack |
207 | |||||
208 | # spent 42µs within NetAddr::IP::Lite::Zeros which was called 9 times, avg 5µs/call:
# 3 times (12µs+0s) by NetAddr::IP::BEGIN@8 at line 666, avg 4µs/call
# 2 times (14µs+0s) by NetAddr::IP::BEGIN@8 at line 650, avg 7µs/call
# 2 times (8µs+0s) by NetAddr::IP::BEGIN@8 at line 657, avg 4µs/call
# 2 times (8µs+0s) by NetAddr::IP::BEGIN@8 at line 674, avg 4µs/call | ||||
209 | 9 | 101µs | return $_zero; | ||
210 | } | ||||
211 | # spent 68µs within NetAddr::IP::Lite::Ones which was called 15 times, avg 5µs/call:
# 6 times (29µs+0s) by NetAddr::IP::Lite::_xnew at line 889, avg 5µs/call
# 3 times (12µs+0s) by NetAddr::IP::BEGIN@8 at line 674, avg 4µs/call
# 2 times (9µs+0s) by NetAddr::IP::Lite::_xnew at line 879, avg 5µs/call
# 2 times (9µs+0s) by NetAddr::IP::BEGIN@8 at line 657, avg 4µs/call
# 2 times (9µs+0s) by NetAddr::IP::Lite::_xnew at line 870, avg 4µs/call | ||||
212 | 15 | 171µs | return $_ones; | ||
213 | } | ||||
214 | sub V4mask() { | ||||
215 | return $_v4mask; | ||||
216 | } | ||||
217 | sub V4net() { | ||||
218 | return $_v4net; | ||||
219 | } | ||||
220 | |||||
221 | ############################################# | ||||
222 | # These are the overload methods, placed here | ||||
223 | # for convenience. | ||||
224 | ############################################# | ||||
225 | |||||
226 | use overload | ||||
227 | |||||
228 | # spent 788µs (72+716) within NetAddr::IP::Lite::BEGIN@228 which was called:
# once (72µs+716µs) by NetAddr::IP::BEGIN@8 at line 280 | ||||
229 | |||||
230 | '-' => \&minus, | ||||
231 | |||||
232 | '++' => \&plusplus, | ||||
233 | |||||
234 | '--' => \&minusminus, | ||||
235 | |||||
236 | "=" => \©, | ||||
237 | |||||
238 | 4 | 85µs | 4 | 1.54ms | # spent 1.61ms (72µs+1.54) within NetAddr::IP::Lite::__ANON__[/usr/local/lib/perl5/site_perl/mach/5.24/NetAddr/IP/Lite.pm:238] which was called 4 times, avg 403µs/call:
# 2 times (20µs+1.32ms) by Mail::SpamAssassin::NetSet::is_net_declared at line 246 of Mail/SpamAssassin/NetSet.pm, avg 672µs/call
# 2 times (52µs+214µs) by Mail::SpamAssassin::NetSet::CORE:match at line 213 of Mail/SpamAssassin/NetSet.pm, avg 133µs/call # spent 1.54ms making 4 calls to NetAddr::IP::Lite::cidr, avg 385µs/call |
239 | |||||
240 | 'eq' => sub { | ||||
241 | my $a = (UNIVERSAL::isa($_[0],__PACKAGE__)) ? $_[0]->cidr : $_[0]; | ||||
242 | my $b = (UNIVERSAL::isa($_[1],__PACKAGE__)) ? $_[1]->cidr : $_[1]; | ||||
243 | $a eq $b; | ||||
244 | }, | ||||
245 | |||||
246 | 'ne' => sub { | ||||
247 | my $a = (UNIVERSAL::isa($_[0],__PACKAGE__)) ? $_[0]->cidr : $_[0]; | ||||
248 | my $b = (UNIVERSAL::isa($_[1],__PACKAGE__)) ? $_[1]->cidr : $_[1]; | ||||
249 | $a ne $b; | ||||
250 | }, | ||||
251 | |||||
252 | '==' => sub { | ||||
253 | return 0 unless UNIVERSAL::isa($_[0],__PACKAGE__) && UNIVERSAL::isa($_[1],__PACKAGE__); | ||||
254 | $_[0]->cidr eq $_[1]->cidr; | ||||
255 | }, | ||||
256 | |||||
257 | '!=' => sub { | ||||
258 | return 1 unless UNIVERSAL::isa($_[0],__PACKAGE__) && UNIVERSAL::isa($_[1],__PACKAGE__); | ||||
259 | $_[0]->cidr ne $_[1]->cidr; | ||||
260 | }, | ||||
261 | |||||
262 | '>' => sub { | ||||
263 | return &comp_addr_mask > 0 ? 1 : 0; | ||||
264 | }, | ||||
265 | |||||
266 | '<' => sub { | ||||
267 | return &comp_addr_mask < 0 ? 1 : 0; | ||||
268 | }, | ||||
269 | |||||
270 | '>=' => sub { | ||||
271 | return &comp_addr_mask < 0 ? 0 : 1; | ||||
272 | }, | ||||
273 | |||||
274 | '<=' => sub { | ||||
275 | return &comp_addr_mask > 0 ? 0 : 1; | ||||
276 | }, | ||||
277 | |||||
278 | 1 | 2µs | '<=>' => \&comp_addr_mask, | ||
279 | |||||
280 | 1 | 12.6ms | 2 | 1.50ms | 'cmp' => \&comp_addr_mask; # spent 788µs making 1 call to NetAddr::IP::Lite::BEGIN@228
# spent 716µs making 1 call to overload::import |
281 | |||||
282 | sub comp_addr_mask { | ||||
283 | my($c,$rv) = sub128($_[0]->{addr},$_[1]->{addr}); | ||||
284 | return -1 unless $c; | ||||
285 | return 1 if hasbits($rv); | ||||
286 | ($c,$rv) = sub128($_[0]->{mask},$_[1]->{mask}); | ||||
287 | return -1 unless $c; | ||||
288 | return hasbits($rv) ? 1 : 0; | ||||
289 | } | ||||
290 | |||||
291 | #sub comp_addr { | ||||
292 | # my($c,$rv) = sub128($_[0]->{addr},$_[1]->{addr}); | ||||
293 | # return -1 unless $c; | ||||
294 | # return hasbits($rv) ? 1 : 0; | ||||
295 | #} | ||||
296 | |||||
297 | =pod | ||||
298 | |||||
299 | =over | ||||
300 | |||||
301 | =item B<Assignment (C<=>)> | ||||
302 | |||||
303 | Has been optimized to copy one NetAddr::IP::Lite object to another very quickly. | ||||
304 | |||||
305 | =item B<C<-E<gt>copy()>> | ||||
306 | |||||
307 | The B<assignment (C<=>)> operation is only put in to operation when the | ||||
308 | copied object is further mutated by another overloaded operation. See | ||||
309 | L<overload> B<SPECIAL SYMBOLS FOR "use overload"> for details. | ||||
310 | |||||
311 | B<C<-E<gt>copy()>> actually creates a new object when called. | ||||
312 | |||||
313 | =cut | ||||
314 | |||||
315 | sub copy { | ||||
316 | return _new($_[0],$_[0]->{addr}, $_[0]->{mask}); | ||||
317 | } | ||||
318 | |||||
319 | =item B<Stringification> | ||||
320 | |||||
321 | An object can be used just as a string. For instance, the following code | ||||
322 | |||||
323 | my $ip = new NetAddr::IP::Lite '192.168.1.123'; | ||||
324 | print "$ip\n"; | ||||
325 | |||||
326 | Will print the string 192.168.1.123/32. | ||||
327 | |||||
328 | my $ip = new6 NetAddr::IP::Lite '192.168.1.123'; | ||||
329 | print "$ip\n"; | ||||
330 | |||||
331 | Will print the string 0:0:0:0:0:0:C0A8:17B/128 | ||||
332 | |||||
333 | =item B<Equality> | ||||
334 | |||||
335 | You can test for equality with either C<eq>, C<ne>, C<==> or C<!=>. C<eq>, C<ne> allows the | ||||
336 | comparison with arbitrary strings as well as NetAddr::IP::Lite objects. The | ||||
337 | following example: | ||||
338 | |||||
339 | if (NetAddr::IP::Lite->new('127.0.0.1','255.0.0.0') eq '127.0.0.1/8') | ||||
340 | { print "Yes\n"; } | ||||
341 | |||||
342 | Will print out "Yes". | ||||
343 | |||||
344 | Comparison with C<==> and C<!=> requires both operands to be NetAddr::IP::Lite objects. | ||||
345 | |||||
346 | =item B<Comparison via E<gt>, E<lt>, E<gt>=, E<lt>=, E<lt>=E<gt> and C<cmp>> | ||||
347 | |||||
348 | Internally, all network objects are represented in 128 bit format. | ||||
349 | The numeric representation of the network is compared through the | ||||
350 | corresponding operation. Comparisons are tried first on the address portion | ||||
351 | of the object and if that is equal then the NUMERIC cidr portion of the | ||||
352 | masks are compared. This leads to the counterintuitive result that | ||||
353 | |||||
354 | /24 > /16 | ||||
355 | |||||
356 | Comparison should not be done on netaddr objects with different CIDR as | ||||
357 | this may produce indeterminate - unexpected results, | ||||
358 | rather the determination of which netblock is larger or smaller should be | ||||
359 | done by comparing | ||||
360 | |||||
361 | $ip1->masklen <=> $ip2->masklen | ||||
362 | |||||
363 | =item B<Addition of a constant (C<+>)> | ||||
364 | |||||
365 | Add a 32 bit signed constant to the address part of a NetAddr object. | ||||
366 | This operation changes the address part to point so many hosts above the | ||||
367 | current objects start address. For instance, this code: | ||||
368 | |||||
369 | print NetAddr::IP::Lite->new('127.0.0.1/8') + 5; | ||||
370 | |||||
371 | will output 127.0.0.6/8. The address will wrap around at the broadcast | ||||
372 | back to the network address. This code: | ||||
373 | |||||
374 | print NetAddr::IP::Lite->new('10.0.0.1/24') + 255; | ||||
375 | |||||
376 | outputs 10.0.0.0/24. | ||||
377 | |||||
378 | Returns the the unchanged object when the constant is missing or out of range. | ||||
379 | |||||
380 | 2147483647 <= constant >= -2147483648 | ||||
381 | |||||
382 | =cut | ||||
383 | |||||
384 | # spent 20µs within NetAddr::IP::Lite::plus which was called 2 times, avg 10µs/call:
# 2 times (20µs+0s) by Mail::SpamAssassin::NetSet::_convert_ipv4_cidr_to_ipv6 at line 208 of Mail/SpamAssassin/NetSet.pm, avg 10µs/call | ||||
385 | 2 | 4µs | my $ip = shift; | ||
386 | 2 | 4µs | my $const = shift; | ||
387 | |||||
388 | 2 | 23µs | return $ip unless $const && | ||
389 | $const < 2147483648 && | ||||
390 | $const > -2147483649; | ||||
391 | |||||
392 | my $a = $ip->{addr}; | ||||
393 | my $m = $ip->{mask}; | ||||
394 | |||||
395 | my $lo = $a & ~$m; | ||||
396 | my $hi = $a & $m; | ||||
397 | |||||
398 | my $new = ((addconst($lo,$const))[1] & ~$m) | $hi; | ||||
399 | |||||
400 | return _new($ip,$new,$m); | ||||
401 | } | ||||
402 | |||||
403 | =item B<Subtraction of a constant (C<->)> | ||||
404 | |||||
405 | The complement of the addition of a constant. | ||||
406 | |||||
407 | =item B<Difference (C<->)> | ||||
408 | |||||
409 | Returns the difference between the address parts of two NetAddr::IP::Lite | ||||
410 | objects address parts as a 32 bit signed number. | ||||
411 | |||||
412 | Returns B<undef> if the difference is out of range. | ||||
413 | |||||
414 | =cut | ||||
415 | |||||
416 | 1 | 2µs | 1 | 5µs | my $_smsk = pack('L3N',0xffffffff,0xffffffff,0xffffffff,0x80000000); # spent 5µs making 1 call to main::CORE:pack |
417 | |||||
418 | sub minus { | ||||
419 | my $ip = shift; | ||||
420 | my $arg = shift; | ||||
421 | unless (ref $arg) { | ||||
422 | return plus($ip, -$arg); | ||||
423 | } | ||||
424 | my($carry,$dif) = sub128($ip->{addr},$arg->{addr}); | ||||
425 | if ($carry) { # value is positive | ||||
426 | return undef if hasbits($dif & $_smsk); # all sign bits should be 0's | ||||
427 | return (unpack('L3N',$dif))[3]; | ||||
428 | } else { | ||||
429 | return undef if hasbits(($dif & $_smsk) ^ $_smsk); # sign is 1's | ||||
430 | return (unpack('L3N',$dif))[3] - 4294967296; | ||||
431 | } | ||||
432 | } | ||||
433 | |||||
434 | # Auto-increment an object | ||||
435 | |||||
436 | =item B<Auto-increment> | ||||
437 | |||||
438 | Auto-incrementing a NetAddr::IP::Lite object causes the address part to be | ||||
439 | adjusted to the next host address within the subnet. It will wrap at | ||||
440 | the broadcast address and start again from the network address. | ||||
441 | |||||
442 | =cut | ||||
443 | |||||
444 | sub plusplus { | ||||
445 | my $ip = shift; | ||||
446 | |||||
447 | my $a = $ip->{addr}; | ||||
448 | my $m = $ip->{mask}; | ||||
449 | |||||
450 | my $lo = $a & ~ $m; | ||||
451 | my $hi = $a & $m; | ||||
452 | |||||
453 | $ip->{addr} = ((addconst($lo,1))[1] & ~ $m) | $hi; | ||||
454 | return $ip; | ||||
455 | } | ||||
456 | |||||
457 | =item B<Auto-decrement> | ||||
458 | |||||
459 | Auto-decrementing a NetAddr::IP::Lite object performs exactly the opposite | ||||
460 | of auto-incrementing it, as you would expect. | ||||
461 | |||||
462 | =cut | ||||
463 | |||||
464 | sub minusminus { | ||||
465 | my $ip = shift; | ||||
466 | |||||
467 | my $a = $ip->{addr}; | ||||
468 | my $m = $ip->{mask}; | ||||
469 | |||||
470 | my $lo = $a & ~$m; | ||||
471 | my $hi = $a & $m; | ||||
472 | |||||
473 | $ip->{addr} = ((addconst($lo,-1))[1] & ~$m) | $hi; | ||||
474 | return $ip; | ||||
475 | } | ||||
476 | |||||
477 | ############################################# | ||||
478 | # End of the overload methods. | ||||
479 | ############################################# | ||||
480 | |||||
481 | # Preloaded methods go here. | ||||
482 | |||||
483 | # This is a variant to ->new() that | ||||
484 | # creates and blesses a new object | ||||
485 | # without the fancy parsing of | ||||
486 | # IP formats and shorthands. | ||||
487 | |||||
488 | # return a blessed IP object without parsing | ||||
489 | # input: prototype, naddr, nmask | ||||
490 | # returns: blessed IP object | ||||
491 | # | ||||
492 | # spent 137µs within NetAddr::IP::Lite::_new which was called 6 times, avg 23µs/call:
# 6 times (137µs+0s) by NetAddr::IP::Lite::network at line 1088, avg 23µs/call | ||||
493 | 6 | 12µs | my $proto = shift; | ||
494 | 6 | 17µs | my $class = ref($proto) || die "reference required"; | ||
495 | 6 | 14µs | $proto = $proto->{isv6}; | ||
496 | 6 | 46µs | my $self = { | ||
497 | addr => $_[0], | ||||
498 | mask => $_[1], | ||||
499 | isv6 => $proto, | ||||
500 | }; | ||||
501 | 6 | 47µs | return bless $self, $class; | ||
502 | } | ||||
503 | |||||
504 | =pod | ||||
505 | |||||
506 | =back | ||||
507 | |||||
508 | =head2 Methods | ||||
509 | |||||
510 | =over | ||||
511 | |||||
512 | =item C<-E<gt>new([$addr, [ $mask|IPv6 ]])> | ||||
513 | |||||
514 | =item C<-E<gt>new6([$addr, [ $mask]])> | ||||
515 | |||||
516 | =item C<-E<gt>new6FFFF([$addr, [ $mask]])> | ||||
517 | |||||
518 | =item C<-E<gt>new_no([$addr, [ $mask]])> | ||||
519 | |||||
520 | =item C<-E<gt>new_from_aton($netaddr)> | ||||
521 | |||||
522 | =item new_cis and new_cis6 are DEPRECATED | ||||
523 | |||||
524 | =item C<-E<gt>new_cis("$addr $mask)> | ||||
525 | |||||
526 | =item C<-E<gt>new_cis6("$addr $mask)> | ||||
527 | |||||
528 | The first three methods create a new address with the supplied address in | ||||
529 | C<$addr> and an optional netmask C<$mask>, which can be omitted to get | ||||
530 | a /32 or /128 netmask for IPv4 / IPv6 addresses respectively. | ||||
531 | |||||
532 | new6FFFF specifically returns an IPv4 address in IPv6 format according to RFC4291 | ||||
533 | |||||
534 | new6 ::xxxx:xxxx | ||||
535 | new6FFFF ::FFFF:xxxx:xxxx | ||||
536 | |||||
537 | The third method C<new_no> is exclusively for IPv4 addresses and filters | ||||
538 | improperly formatted | ||||
539 | dot quad strings for leading 0's that would normally be interpreted as octal | ||||
540 | format by NetAddr per the specifications for inet_aton. | ||||
541 | |||||
542 | B<new_from_aton> takes a packed IPv4 address and assumes a /32 mask. This | ||||
543 | function replaces the DEPRECATED :aton functionality which is fundamentally | ||||
544 | broken. | ||||
545 | |||||
546 | The last two methods B<new_cis> and B<new_cis6> differ from B<new> and | ||||
547 | B<new6> only in that they except the common Cisco address notation for | ||||
548 | address/mask pairs with a B<space> as a separator instead of a slash (/) | ||||
549 | |||||
550 | These methods are DEPRECATED because the functionality is now included | ||||
551 | in the other "new" methods | ||||
552 | |||||
553 | i.e. ->new_cis('1.2.3.0 24') | ||||
554 | or | ||||
555 | ->new_cis6('::1.2.3.0 120') | ||||
556 | |||||
557 | C<-E<gt>new6> and | ||||
558 | C<-E<gt>new_cis6> mark the address as being in ipV6 address space even | ||||
559 | if the format would suggest otherwise. | ||||
560 | |||||
561 | i.e. ->new6('1.2.3.4') will result in ::102:304 | ||||
562 | |||||
563 | addresses submitted to ->new in ipV6 notation will | ||||
564 | remain in that notation permanently. i.e. | ||||
565 | ->new('::1.2.3.4') will result in ::102:304 | ||||
566 | whereas new('1.2.3.4') would print out as 1.2.3.4 | ||||
567 | |||||
568 | See "STRINGIFICATION" below. | ||||
569 | |||||
570 | C<$addr> can be almost anything that can be resolved to an IP address | ||||
571 | in all the notations I have seen over time. It can optionally contain | ||||
572 | the mask in CIDR notation. If the OPTIONAL perl module Socket6 is | ||||
573 | available in the local library it will autoload and ipV6 host6 | ||||
574 | names will be resolved as well as ipV4 hostnames. | ||||
575 | |||||
576 | B<prefix> notation is understood, with the limitation that the range | ||||
577 | specified by the prefix must match with a valid subnet. | ||||
578 | |||||
579 | Addresses in the same format returned by C<inet_aton> or | ||||
580 | C<gethostbyname> can also be understood, although no mask can be | ||||
581 | specified for them. The default is to not attempt to recognize this | ||||
582 | format, as it seems to be seldom used. | ||||
583 | |||||
584 | ###### DEPRECATED, will be remove in version 5 ############ | ||||
585 | To accept addresses in that format, invoke the module as in | ||||
586 | |||||
587 | use NetAddr::IP::Lite ':aton' | ||||
588 | |||||
589 | ###### USE new_from_aton instead ########################## | ||||
590 | |||||
591 | If called with no arguments, 'default' is assumed. | ||||
592 | |||||
593 | If called with an empty string as the argument, returns 'undef' | ||||
594 | |||||
595 | C<$addr> can be any of the following and possibly more... | ||||
596 | |||||
597 | n.n | ||||
598 | n.n/mm | ||||
599 | n.n mm | ||||
600 | n.n.n | ||||
601 | n.n.n/mm | ||||
602 | n.n.n mm | ||||
603 | n.n.n.n | ||||
604 | n.n.n.n/mm 32 bit cidr notation | ||||
605 | n.n.n.n mm | ||||
606 | n.n.n.n/m.m.m.m | ||||
607 | n.n.n.n m.m.m.m | ||||
608 | loopback, localhost, broadcast, any, default | ||||
609 | x.x.x.x/host | ||||
610 | 0xABCDEF, 0b111111000101011110, (or a bcd number) | ||||
611 | a netaddr as returned by 'inet_aton' | ||||
612 | |||||
613 | |||||
614 | Any RFC1884 notation | ||||
615 | |||||
616 | ::n.n.n.n | ||||
617 | ::n.n.n.n/mmm 128 bit cidr notation | ||||
618 | ::n.n.n.n/::m.m.m.m | ||||
619 | ::x:x | ||||
620 | ::x:x/mmm | ||||
621 | x:x:x:x:x:x:x:x | ||||
622 | x:x:x:x:x:x:x:x/mmm | ||||
623 | x:x:x:x:x:x:x:x/m:m:m:m:m:m:m:m any RFC1884 notation | ||||
624 | loopback, localhost, unspecified, any, default | ||||
625 | ::x:x/host | ||||
626 | 0xABCDEF, 0b111111000101011110 within the limits | ||||
627 | of perl's number resolution | ||||
628 | 123456789012 a 'big' bcd number (bigger than perl likes) | ||||
629 | and Math::BigInt | ||||
630 | |||||
631 | A Fully Qualified Domain Name which returns an ipV4 address or an ipV6 | ||||
632 | address, embodied in that order. This previously undocumented feature | ||||
633 | may be disabled with: | ||||
634 | |||||
635 | use NetAddr::IP::Lite ':nofqdn'; | ||||
636 | |||||
637 | If called with no arguments, 'default' is assumed. | ||||
638 | |||||
639 | If called with and empty string as the argument, 'undef' is returned; | ||||
640 | |||||
641 | =cut | ||||
642 | |||||
643 | 1 | 12µs | 1 | 147µs | my $lbmask = inet_aton('255.0.0.0'); # spent 147µs making 1 call to NetAddr::IP::InetBase::inet_aton |
644 | 1 | 19µs | 1 | 643µs | my $_p4broad = inet_any2n('255.255.255.255'); # spent 643µs making 1 call to AutoLoader::AUTOLOAD |
645 | 1 | 7µs | 1 | 307µs | my $_p4loop = inet_any2n('127.0.0.1'); # spent 307µs making 1 call to NetAddr::IP::InetBase::inet_any2n |
646 | 1 | 7µs | 1 | 99µs | my $_p4mloop = inet_aton('255.0.0.0'); # spent 99µs making 1 call to NetAddr::IP::InetBase::inet_aton |
647 | 1 | 25µs | 1 | 12µs | $_p4mloop = mask4to6($_p4mloop); # spent 12µs making 1 call to NetAddr::IP::Util::mask4to6 |
648 | 1 | 7µs | 1 | 302µs | my $_p6loop = inet_any2n('::1'); # spent 302µs making 1 call to NetAddr::IP::InetBase::inet_any2n |
649 | |||||
650 | 1 | 32µs | 2 | 14µs | my %fip4 = ( # spent 14µs making 2 calls to NetAddr::IP::Lite::Zeros, avg 7µs/call |
651 | default => Zeros, | ||||
652 | any => Zeros, | ||||
653 | broadcast => $_p4broad, | ||||
654 | loopback => $_p4loop, | ||||
655 | unspecified => undef, | ||||
656 | ); | ||||
657 | 1 | 24µs | 4 | 17µs | my %fip4m = ( # spent 9µs making 2 calls to NetAddr::IP::Lite::Ones, avg 4µs/call
# spent 8µs making 2 calls to NetAddr::IP::Lite::Zeros, avg 4µs/call |
658 | default => Zeros, | ||||
659 | any => Zeros, | ||||
660 | broadcast => Ones, | ||||
661 | loopback => $_p4mloop, | ||||
662 | unspecified => undef, # not applicable for ipV4 | ||||
663 | host => Ones, | ||||
664 | ); | ||||
665 | |||||
666 | 1 | 32µs | 3 | 12µs | my %fip6 = ( # spent 12µs making 3 calls to NetAddr::IP::Lite::Zeros, avg 4µs/call |
667 | default => Zeros, | ||||
668 | any => Zeros, | ||||
669 | broadcast => undef, # not applicable for ipV6 | ||||
670 | loopback => $_p6loop, | ||||
671 | unspecified => Zeros, | ||||
672 | ); | ||||
673 | |||||
674 | 1 | 25µs | 5 | 20µs | my %fip6m = ( # spent 12µs making 3 calls to NetAddr::IP::Lite::Ones, avg 4µs/call
# spent 8µs making 2 calls to NetAddr::IP::Lite::Zeros, avg 4µs/call |
675 | default => Zeros, | ||||
676 | any => Zeros, | ||||
677 | broadcast => undef, # not applicable for ipV6 | ||||
678 | loopback => Ones, | ||||
679 | unspecified => Ones, | ||||
680 | host => Ones, | ||||
681 | ); | ||||
682 | |||||
683 | 1 | 2µs | 1 | 4µs | my $ff000000 = pack('L3N',0xffffffff,0xffffffff,0xffffffff,0xFF000000); # spent 4µs making 1 call to main::CORE:pack |
684 | 1 | 2µs | 1 | 3µs | my $ffff0000 = pack('L3N',0xffffffff,0xffffffff,0xffffffff,0xFFFF0000); # spent 3µs making 1 call to main::CORE:pack |
685 | 1 | 2µs | 1 | 3µs | my $ffffff00 = pack('L3N',0xffffffff,0xffffffff,0xffffffff,0xFFFFFF00); # spent 3µs making 1 call to main::CORE:pack |
686 | |||||
687 | sub _obits ($$) { | ||||
688 | my($lo,$hi) = @_; | ||||
689 | |||||
690 | return 0xFF if $lo == $hi; | ||||
691 | return (~ ($hi ^ $lo)) & 0xFF; | ||||
692 | } | ||||
693 | |||||
694 | sub new_no($;$$) { | ||||
695 | unshift @_, -1; | ||||
696 | goto &_xnew; | ||||
697 | } | ||||
698 | |||||
699 | # spent 72µs within NetAddr::IP::Lite::new which was called 9 times, avg 8µs/call:
# 4 times (34µs+0s) by Mail::SpamAssassin::NetSet::add_cidr at line 154 of Mail/SpamAssassin/NetSet.pm, avg 9µs/call
# once (11µs+0s) by NetAddr::IP::BEGIN@8 at line 1374
# once (7µs+0s) by NetAddr::IP::BEGIN@8 at line 1378
# once (7µs+0s) by NetAddr::IP::BEGIN@8 at line 1404
# once (7µs+0s) by NetAddr::IP::BEGIN@8 at line 1382
# once (7µs+0s) by NetAddr::IP::BEGIN@8 at line 1405 | ||||
700 | 9 | 26µs | unshift @_, 0; | ||
701 | 9 | 160µs | 9 | 4.09ms | goto &_xnew; # spent 4.09ms making 9 calls to NetAddr::IP::Lite::_xnew, avg 455µs/call |
702 | } | ||||
703 | |||||
704 | sub new_from_aton($$) { | ||||
705 | my $proto = shift; | ||||
706 | my $class = ref $proto || $proto || __PACKAGE__; | ||||
707 | my $ip = shift; | ||||
708 | return undef unless defined $ip; | ||||
709 | my $addrlen = length($ip); | ||||
710 | return undef unless $addrlen == 4; | ||||
711 | my $self = { | ||||
712 | addr => ipv4to6($ip), | ||||
713 | mask => &Ones, | ||||
714 | isv6 => 0, | ||||
715 | }; | ||||
716 | return bless $self, $class; | ||||
717 | } | ||||
718 | |||||
719 | # spent 101µs within NetAddr::IP::Lite::new6 which was called 10 times, avg 10µs/call:
# 6 times (69µs+0s) by Mail::SpamAssassin::Util::reverse_ip_address at line 914 of Mail/SpamAssassin/Util.pm, avg 12µs/call
# 2 times (16µs+0s) by Mail::SpamAssassin::NetSet::_convert_ipv4_cidr_to_ipv6 at line 208 of Mail/SpamAssassin/NetSet.pm, avg 8µs/call
# 2 times (15µs+0s) by Mail::SpamAssassin::NetSet::_convert_ipv4_cidr_to_ipv6 at line 218 of Mail/SpamAssassin/NetSet.pm, avg 8µs/call | ||||
720 | 10 | 32µs | unshift @_, 1; | ||
721 | 10 | 181µs | 10 | 5.13ms | goto &_xnew; # spent 5.13ms making 10 calls to NetAddr::IP::Lite::_xnew, avg 513µs/call |
722 | } | ||||
723 | |||||
724 | sub new6FFFF($;$$) { | ||||
725 | my $ip = _xnew(1,@_); | ||||
726 | $ip->{addr} |= $_ipv4FFFF; | ||||
727 | return $ip; | ||||
728 | } | ||||
729 | |||||
730 | sub new_cis($;$$) { | ||||
731 | my @in = @_; | ||||
732 | if ( $in[1] && $in[1] =~ m!^(.+)\s+(.+)$! ) { | ||||
733 | $in[1] = $1 .'/'. $2; | ||||
734 | } | ||||
735 | @_ = (0,@in); | ||||
736 | goto &_xnew; | ||||
737 | } | ||||
738 | |||||
739 | sub new_cis6($;$$) { | ||||
740 | my @in = @_; | ||||
741 | if ( $in[1] && $in[1] =~ m!^(.+)\s+(.+)$! ) { | ||||
742 | $in[1] = $1 .'/'. $2; | ||||
743 | } | ||||
744 | @_ = (1,@in); | ||||
745 | goto &_xnew; | ||||
746 | } | ||||
747 | |||||
748 | sub _no_octal { | ||||
749 | $_[0] =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; | ||||
750 | return sprintf("%d.%d.%d.%d",$1,$2,$3,$4); | ||||
751 | } | ||||
752 | |||||
753 | # spent 9.22ms (3.45+5.77) within NetAddr::IP::Lite::_xnew which was called 19 times, avg 485µs/call:
# 10 times (1.78ms+3.35ms) by Mail::SpamAssassin::NetSet::_convert_ipv4_cidr_to_ipv6 or Mail::SpamAssassin::Util::reverse_ip_address at line 721, avg 513µs/call
# 9 times (1.67ms+2.42ms) by Mail::SpamAssassin::NetSet::add_cidr or NetAddr::IP::BEGIN@8 at line 701, avg 455µs/call | ||||
754 | 19 | 38µs | my $noctal = 0; | ||
755 | 19 | 42µs | my $isV6 = shift; | ||
756 | 19 | 33µs | if ($isV6 < 0) { # flag for no octal? | ||
757 | $isV6 = 0; | ||||
758 | $noctal = 1; | ||||
759 | } | ||||
760 | 19 | 38µs | my $proto = shift; | ||
761 | 19 | 39µs | my $class = ref $proto || $proto || __PACKAGE__; | ||
762 | 19 | 41µs | my $ip = shift; | ||
763 | |||||
764 | # fix for bug #75976 | ||||
765 | 19 | 42µs | return undef if defined $ip && $ip eq ''; | ||
766 | |||||
767 | 19 | 39µs | $ip = 'default' unless defined $ip; | ||
768 | 19 | 35µs | $ip = _retMBIstring($ip) # treat as big bcd string | ||
769 | if ref $ip && ref $ip eq 'Math::BigInt'; # can /CIDR notation | ||||
770 | 19 | 33µs | my $hasmask = 1; | ||
771 | 19 | 31µs | my($mask,$tmp); | ||
772 | |||||
773 | # IP to lower case AFTER ref test for Math::BigInt. 'lc' strips blessing | ||||
774 | |||||
775 | 19 | 76µs | $ip = lc $ip; | ||
776 | |||||
777 | 19 | 31µs | while (1) { | ||
778 | # process IP's with no CIDR or that have the CIDR as part of the IP argument string | ||||
779 | 19 | 72µs | unless (@_) { | ||
780 | # if ($ip =~ m!^(.+)/(.+)$!) { | ||||
781 | 19 | 716µs | 47 | 251µs | if ($ip !~ /\D/) { # binary number notation # spent 251µs making 47 calls to NetAddr::IP::Lite::CORE:match, avg 5µs/call |
782 | $ip = bcd2bin($ip); | ||||
783 | $mask = Ones; | ||||
784 | last; | ||||
785 | } | ||||
786 | elsif ($ip =~ m!^([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)$! || | ||||
787 | $ip =~ m!^[\[]{1}([a-z0-9.:-]+)(?:/|\s+)([a-z0-9.:-]+)[\]]{1}$!) { | ||||
788 | 10 | 27µs | $ip = $1; | ||
789 | 10 | 22µs | $mask = $2; | ||
790 | } elsif (grep($ip eq $_,(qw(default any broadcast loopback unspecified)))) { | ||||
791 | $isV6 = 1 if $ip eq 'unspecified'; | ||||
792 | if ($isV6) { | ||||
793 | $mask = $fip6m{$ip}; | ||||
794 | return undef unless defined ($ip = $fip6{$ip}); | ||||
795 | } else { | ||||
796 | $mask = $fip4m{$ip}; | ||||
797 | return undef unless defined ($ip = $fip4{$ip}); | ||||
798 | } | ||||
799 | last; | ||||
800 | } | ||||
801 | } | ||||
802 | # process "ipv6" token and default IP's | ||||
803 | elsif (defined $_[0]) { | ||||
804 | if ($_[0] =~ /ipv6/i || $isV6) { | ||||
805 | if (grep($ip eq $_,(qw(default any loopback unspecified)))) { | ||||
806 | $mask = $fip6m{$ip}; | ||||
807 | $ip = $fip6{$ip}; | ||||
808 | last; | ||||
809 | } else { | ||||
810 | return undef unless $isV6; | ||||
811 | # add for ipv6 notation "12345, 1" | ||||
812 | } | ||||
813 | # $mask = lc $_[0]; | ||||
814 | # } else { | ||||
815 | # $mask = lc $_[0]; | ||||
816 | } | ||||
817 | # extract mask | ||||
818 | $mask = $_[0]; | ||||
819 | } | ||||
820 | ### | ||||
821 | ### process mask | ||||
822 | 19 | 61µs | unless (defined $mask) { | ||
823 | 9 | 25µs | $hasmask = 0; | ||
824 | 9 | 19µs | $mask = 'host'; | ||
825 | } | ||||
826 | |||||
827 | # two kinds of IP's can turn on the isV6 flag | ||||
828 | # 1) big digits that are over the IPv4 boundry | ||||
829 | # 2) IPv6 IP syntax | ||||
830 | # | ||||
831 | # check these conditions and set isV6 as appropriate | ||||
832 | # | ||||
833 | 19 | 31µs | my $try; | ||
834 | 19 | 396µs | 30 | 2.63ms | $isV6 = 1 if # check big bcd and IPv6 rfc1884 # spent 2.53ms making 11 calls to NetAddr::IP::InetBase::ipv6_aton, avg 230µs/call
# spent 90µs making 19 calls to NetAddr::IP::Lite::CORE:match, avg 5µs/call |
835 | ( $ip !~ /\D/ && # ip is all decimal | ||||
836 | (length($ip) > 3 || $ip > 255) && # exclude a single digit in the range of zero to 255, could be funny IPv4 | ||||
837 | ($try = bcd2bin($ip)) && ! isIPv4($try)) || # precedence so $try is not corrupted | ||||
838 | (index($ip,':') >= 0 && ($try = ipv6_aton($ip))); # fails if not an rfc1884 address | ||||
839 | |||||
840 | # if either of the above conditions is true, $try contains the NetAddr 128 bit address | ||||
841 | |||||
842 | # checkfor Math::BigInt mask | ||||
843 | 19 | 32µs | $mask = _retMBIstring($mask) # treat as big bcd string | ||
844 | if ref $mask && ref $mask eq 'Math::BigInt'; | ||||
845 | |||||
846 | # MASK to lower case AFTER ref test for Math::BigInt, 'lc' strips blessing | ||||
847 | |||||
848 | 19 | 42µs | $mask = lc $mask; | ||
849 | |||||
850 | 19 | 352µs | 28 | 77µs | if ($mask !~ /\D/) { # bcd or CIDR notation # spent 77µs making 28 calls to NetAddr::IP::Lite::CORE:match, avg 3µs/call |
851 | 10 | 28µs | my $isCIDR = length($mask) < 4 && $mask < 129; | ||
852 | 10 | 42µs | if ($isV6) { | ||
853 | 4 | 12µs | if ($isCIDR) { | ||
854 | 4 | 7µs | my($dq1,$dq2,$dq3,$dq4); | ||
855 | 4 | 68µs | 4 | 16µs | if ($ip =~ /^(\d+)(?:|\.(\d+)(?:|\.(\d+)(?:|\.(\d+))))$/ && # spent 16µs making 4 calls to NetAddr::IP::Lite::CORE:match, avg 4µs/call |
856 | 2 | 5µs | do {$dq1 = $1; | ||
857 | 2 | 6µs | $dq2 = $2 || 0; | ||
858 | 2 | 4µs | $dq3 = $3 || 0; | ||
859 | 2 | 4µs | $dq4 = $4 || 0; | ||
860 | 2 | 3µs | 1; | ||
861 | } && | ||||
862 | $dq1 >= 0 && $dq1 < 256 && | ||||
863 | $dq2 >= 0 && $dq2 < 256 && | ||||
864 | $dq3 >= 0 && $dq3 < 256 && | ||||
865 | $dq4 >= 0 && $dq4 < 256 | ||||
866 | ) { # corner condition of IPv4 with isV6 | ||||
867 | 2 | 9µs | $ip = join('.',$dq1,$dq2,$dq3,$dq4); | ||
868 | 2 | 36µs | 4 | 131µs | $try = ipv4to6(inet_aton($ip)); # spent 125µs making 2 calls to NetAddr::IP::InetBase::inet_aton, avg 62µs/call
# spent 6µs making 2 calls to NetAddr::IP::Util::ipv4to6, avg 3µs/call |
869 | 2 | 7µs | if ($mask < 32) { | ||
870 | 2 | 32µs | 4 | 17µs | $mask = shiftleft(Ones,32 -$mask); # spent 9µs making 2 calls to NetAddr::IP::Lite::Ones, avg 4µs/call
# spent 9µs making 2 calls to NetAddr::IP::Util::shiftleft, avg 4µs/call |
871 | } | ||||
872 | elsif ($mask == 32) { | ||||
873 | $mask = Ones; | ||||
874 | } else { | ||||
875 | return undef; # undoubtably an error | ||||
876 | } | ||||
877 | } | ||||
878 | elsif ($mask < 128) { | ||||
879 | 2 | 35µs | 4 | 18µs | $mask = shiftleft(Ones,128 -$mask); # small cidr # spent 9µs making 2 calls to NetAddr::IP::Lite::Ones, avg 5µs/call
# spent 9µs making 2 calls to NetAddr::IP::Util::shiftleft, avg 4µs/call |
880 | } else { | ||||
881 | $mask = Ones(); | ||||
882 | } | ||||
883 | } else { | ||||
884 | $mask = bcd2bin($mask); | ||||
885 | } | ||||
886 | } | ||||
887 | elsif ($isCIDR && $mask < 33) { # is V4 | ||||
888 | 6 | 20µs | if ($mask < 32) { | ||
889 | 6 | 146µs | 12 | 74µs | $mask = shiftleft(Ones,32 -$mask); # spent 45µs making 6 calls to NetAddr::IP::Util::shiftleft, avg 7µs/call
# spent 29µs making 6 calls to NetAddr::IP::Lite::Ones, avg 5µs/call |
890 | } | ||||
891 | elsif ( $mask == 32) { | ||||
892 | $mask = Ones; | ||||
893 | } else { | ||||
894 | $mask = bcd2bin($mask); | ||||
895 | $mask |= $_v4mask; # v4 always | ||||
896 | } | ||||
897 | } else { # also V4 | ||||
898 | $mask = bcd2bin($mask); | ||||
899 | $mask |= $_v4mask; | ||||
900 | } | ||||
901 | 10 | 34µs | if ($try) { # is a big number | ||
902 | 4 | 7µs | $ip = $try; | ||
903 | 4 | 8µs | last; | ||
904 | } | ||||
905 | } elsif ($mask =~ m/^\d+\.\d+\.\d+\.\d+$/) { # ipv4 form of mask | ||||
906 | $mask = _no_octal($mask) if $noctal; # filter for octal | ||||
907 | return undef unless defined ($mask = inet_aton($mask)); | ||||
908 | $mask = mask4to6($mask); | ||||
909 | } elsif (grep($mask eq $_,qw(default any broadcast loopback unspecified host))) { | ||||
910 | 9 | 48µs | if (index($ip,':') < 0 && ! $isV6) { | ||
911 | return undef unless defined ($mask = $fip4m{$mask}); | ||||
912 | } else { | ||||
913 | 9 | 30µs | return undef unless defined ($mask = $fip6m{$mask}); | ||
914 | } | ||||
915 | } else { | ||||
916 | return undef unless defined ($mask = ipv6_aton($mask)); # try ipv6 form of mask | ||||
917 | } | ||||
918 | |||||
919 | # process remaining IP's | ||||
920 | |||||
921 | 15 | 43µs | if (index($ip,':') < 0) { # ipv4 address | ||
922 | 6 | 115µs | 9 | 40µs | if ($ip =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) { # spent 40µs making 9 calls to NetAddr::IP::Lite::CORE:match, avg 4µs/call |
923 | ; # the common case | ||||
924 | } | ||||
925 | elsif (grep($ip eq $_,(qw(default any broadcast loopback)))) { | ||||
926 | return undef unless defined ($ip = $fip4{$ip}); | ||||
927 | last; | ||||
928 | } | ||||
929 | elsif ($ip =~ m/^(\d+)\.(\d+)$/) { | ||||
930 | $ip = ($hasmask) | ||||
931 | ? "${1}.${2}.0.0" | ||||
932 | : "${1}.0.0.${2}"; | ||||
933 | } | ||||
934 | elsif ($ip =~ m/^(\d+)\.(\d+)\.(\d+)$/) { | ||||
935 | $ip = ($hasmask) | ||||
936 | ? "${1}.${2}.${3}.0" | ||||
937 | : "${1}.${2}.0.${3}"; | ||||
938 | } | ||||
939 | elsif ($ip =~ /^(\d+)$/ && $hasmask && $1 >= 0 and $1 < 256) { # pure numeric | ||||
940 | 1 | 6µs | $ip = sprintf("%d.0.0.0",$1); | ||
941 | } | ||||
942 | # elsif ($ip =~ /^\d+$/ && !$hasmask) { # a big integer | ||||
943 | elsif ($ip =~ /^\d+$/ ) { # a big integer | ||||
944 | $ip = bcd2bin($ip); | ||||
945 | last; | ||||
946 | } | ||||
947 | # these next three might be broken??? but they have been in the code a long time and no one has complained | ||||
948 | elsif ($ip =~ /^0[xb]\d+$/ && $hasmask && | ||||
949 | (($tmp = eval "$ip") || 1) && | ||||
950 | $tmp >= 0 && $tmp < 256) { | ||||
951 | $ip = sprintf("%d.0.0.0",$tmp); | ||||
952 | } | ||||
953 | elsif ($ip =~ /^-?\d+$/) { | ||||
954 | $ip += 2 ** 32 if $ip < 0; | ||||
955 | $ip = pack('L3N',0,0,0,$ip); | ||||
956 | last; | ||||
957 | } | ||||
958 | elsif ($ip =~ /^-?0[xb]\d+$/) { | ||||
959 | $ip = eval "$ip"; | ||||
960 | $ip = pack('L3N',0,0,0,$ip); | ||||
961 | last; | ||||
962 | } | ||||
963 | |||||
964 | # notations below include an implicit mask specification | ||||
965 | |||||
966 | elsif ($ip =~ m/^(\d+)\.$/) { | ||||
967 | $ip = "${1}.0.0.0"; | ||||
968 | $mask = $ff000000; | ||||
969 | } | ||||
970 | elsif ($ip =~ m/^(\d+)\.(\d+)-(\d+)\.?$/ && $2 <= $3 && $3 < 256) { | ||||
971 | $ip = "${1}.${2}.0.0"; | ||||
972 | $mask = pack('L3C4',0xffffffff,0xffffffff,0xffffffff,255,_obits($2,$3),0,0); | ||||
973 | } | ||||
974 | elsif ($ip =~ m/^(\d+)-(\d+)\.?$/ and $1 <= $2 && $2 < 256) { | ||||
975 | $ip = "${1}.0.0.0"; | ||||
976 | $mask = pack('L3C4',0xffffffff,0xffffffff,0xffffffff,_obits($1,$2),0,0,0) | ||||
977 | } | ||||
978 | elsif ($ip =~ m/^(\d+)\.(\d+)\.$/) { | ||||
979 | $ip = "${1}.${2}.0.0"; | ||||
980 | $mask = $ffff0000; | ||||
981 | } | ||||
982 | elsif ($ip =~ m/^(\d+)\.(\d+)\.(\d+)-(\d+)\.?$/ && $3 <= $4 && $4 < 256) { | ||||
983 | $ip = "${1}.${2}.${3}.0"; | ||||
984 | $mask = pack('L3C4',0xffffffff,0xffffffff,0xffffffff,255,255,_obits($3,$4),0); | ||||
985 | } | ||||
986 | elsif ($ip =~ m/^(\d+)\.(\d+)\.(\d+)\.$/) { | ||||
987 | $ip = "${1}.${2}.${3}.0"; | ||||
988 | $mask = $ffffff00; | ||||
989 | } | ||||
990 | elsif ($ip =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)$/ && $4 <= $5 && $5 < 256) { | ||||
991 | $ip = "${1}.${2}.${3}.${4}"; | ||||
992 | $mask = pack('L3C4',0xffffffff,0xffffffff,0xffffffff,255,255,255,_obits($4,$5)); | ||||
993 | } | ||||
994 | elsif ($ip =~ m/^(\d+\.\d+\.\d+\.\d+) | ||||
995 | \s*-\s*(\d+\.\d+\.\d+\.\d+)$/x) { | ||||
996 | if ($noctal) { | ||||
997 | return undef unless ($ip = inet_aton(_no_octal($1))); | ||||
998 | return undef unless ($tmp = inet_aton(_no_octal($2))); | ||||
999 | } else { | ||||
1000 | return undef unless ($ip = inet_aton($1)); | ||||
1001 | return undef unless ($tmp = inet_aton($2)); | ||||
1002 | } | ||||
1003 | # check for left side greater than right side | ||||
1004 | # save numeric difference in $mask | ||||
1005 | return undef if ($tmp = unpack('N',$tmp) - unpack('N',$ip)) < 0; | ||||
1006 | $ip = ipv4to6($ip); | ||||
1007 | $tmp = pack('L3N',0,0,0,$tmp); | ||||
1008 | $mask = ~$tmp; | ||||
1009 | return undef if notcontiguous($mask); | ||||
1010 | # check for non-aligned left side | ||||
1011 | return undef if hasbits($ip & $tmp); | ||||
1012 | last; | ||||
1013 | } | ||||
1014 | # check for resolvable IPv4 hosts | ||||
1015 | elsif (! $NoFQDN && $ip !~ /[^a-zA-Z0-9\._-]/ && ($tmp = gethostbyname(fillIPv4($ip))) && $tmp ne $_v4zero && $tmp ne $_zero ) { | ||||
1016 | $ip = ipv4to6($tmp); | ||||
1017 | last; | ||||
1018 | } | ||||
1019 | # check for resolvable IPv6 hosts | ||||
1020 | elsif (! $NoFQDN && $ip !~ /[^a-zA-Z0-9\._-]/ && havegethostbyname2() && ($tmp = naip_gethostbyname($ip))) { | ||||
1021 | $ip = $tmp; | ||||
1022 | $isV6 = 1; | ||||
1023 | last; | ||||
1024 | } | ||||
1025 | elsif ($Accept_Binary_IP && ! $hasmask) { | ||||
1026 | if (length($ip) == 4) { | ||||
1027 | $ip = ipv4to6($ip); | ||||
1028 | } elsif (length($ip) == 16) { | ||||
1029 | $isV6 = 1; | ||||
1030 | } else { | ||||
1031 | return undef; | ||||
1032 | } | ||||
1033 | last; | ||||
1034 | } else { | ||||
1035 | return undef; | ||||
1036 | } | ||||
1037 | 6 | 41µs | 6 | 468µs | return undef unless defined ($ip = inet_aton($ip)); # spent 468µs making 6 calls to NetAddr::IP::InetBase::inet_aton, avg 78µs/call |
1038 | 6 | 105µs | 6 | 41µs | $ip = ipv4to6($ip); # spent 41µs making 6 calls to NetAddr::IP::Util::ipv4to6, avg 7µs/call |
1039 | 6 | 16µs | last; | ||
1040 | } | ||||
1041 | ########## continuing | ||||
1042 | else { # ipv6 address | ||||
1043 | 9 | 18µs | $isV6 = 1; | ||
1044 | 9 | 92µs | 9 | 22µs | $ip = $1 if $ip =~ /\[([^\]]+)\]/; # transform URI notation # spent 22µs making 9 calls to NetAddr::IP::Lite::CORE:match, avg 2µs/call |
1045 | 9 | 73µs | 9 | 1.92ms | if (defined ($tmp = ipv6_aton($ip))) { # spent 1.92ms making 9 calls to NetAddr::IP::InetBase::ipv6_aton, avg 213µs/call |
1046 | 9 | 19µs | $ip = $tmp; | ||
1047 | 9 | 27µs | last; | ||
1048 | } | ||||
1049 | last if grep($ip eq $_,(qw(default any loopback unspecified))) && | ||||
1050 | defined ($ip = $fip6{$ip}); | ||||
1051 | return undef; | ||||
1052 | } | ||||
1053 | } # end while (1) | ||||
1054 | 19 | 283µs | 19 | 70µs | return undef if notcontiguous($mask); # invalid if not contiguous # spent 70µs making 19 calls to NetAddr::IP::Util::notcontiguous, avg 4µs/call |
1055 | |||||
1056 | 19 | 119µs | my $self = { | ||
1057 | addr => $ip, | ||||
1058 | mask => $mask, | ||||
1059 | isv6 => $isV6, | ||||
1060 | }; | ||||
1061 | 19 | 203µs | return bless $self, $class; | ||
1062 | } | ||||
1063 | |||||
1064 | =item C<-E<gt>broadcast()> | ||||
1065 | |||||
1066 | Returns a new object referring to the broadcast address of a given | ||||
1067 | subnet. The broadcast address has all ones in all the bit positions | ||||
1068 | where the netmask has zero bits. This is normally used to address all | ||||
1069 | the hosts in a given subnet. | ||||
1070 | |||||
1071 | =cut | ||||
1072 | |||||
1073 | sub broadcast ($) { | ||||
1074 | my $ip = _new($_[0],$_[0]->{addr} | ~$_[0]->{mask},$_[0]->{mask}); | ||||
1075 | $ip->{addr} &= V4net unless $ip->{isv6}; | ||||
1076 | return $ip; | ||||
1077 | } | ||||
1078 | |||||
1079 | =item C<-E<gt>network()> | ||||
1080 | |||||
1081 | Returns a new object referring to the network address of a given | ||||
1082 | subnet. A network address has all zero bits where the bits of the | ||||
1083 | netmask are zero. Normally this is used to refer to a subnet. | ||||
1084 | |||||
1085 | =cut | ||||
1086 | |||||
1087 | # spent 344µs (208+137) within NetAddr::IP::Lite::network which was called 6 times, avg 57µs/call:
# 6 times (208µs+137µs) by Mail::SpamAssassin::Util::reverse_ip_address at line 917 of Mail/SpamAssassin/Util.pm, avg 57µs/call | ||||
1088 | 6 | 221µs | 6 | 137µs | return _new($_[0],$_[0]->{addr} & $_[0]->{mask},$_[0]->{mask}); # spent 137µs making 6 calls to NetAddr::IP::Lite::_new, avg 23µs/call |
1089 | } | ||||
1090 | |||||
1091 | =item C<-E<gt>addr()> | ||||
1092 | |||||
1093 | Returns a scalar with the address part of the object as an IPv4 or IPv6 text | ||||
1094 | string as appropriate. This is useful for printing or for passing the address | ||||
1095 | part of the NetAddr::IP::Lite object to other components that expect an IP | ||||
1096 | address. If the object is an ipV6 address or was created using ->new6($ip) | ||||
1097 | it will be reported in ipV6 hex format otherwise it will be reported in dot | ||||
1098 | quad format only if it resides in ipV4 address space. | ||||
1099 | |||||
1100 | =cut | ||||
1101 | |||||
1102 | # spent 1.29ms (80µs+1.20) within NetAddr::IP::Lite::addr which was called 4 times, avg 321µs/call:
# 4 times (80µs+1.20ms) by NetAddr::IP::Lite::cidr at line 1169, avg 321µs/call | ||||
1103 | return ($_[0]->{isv6}) | ||||
1104 | ? ipv6_n2x($_[0]->{addr}) | ||||
1105 | 4 | 83µs | 4 | 1.05ms | : inet_n2dx($_[0]->{addr}); # spent 872µs making 1 call to AutoLoader::AUTOLOAD
# spent 96µs making 1 call to NetAddr::IP::InetBase::inet_n2dx
# spent 79µs making 2 calls to NetAddr::IP::InetBase::ipv6_n2x, avg 40µs/call |
1106 | } | ||||
1107 | |||||
1108 | =item C<-E<gt>mask()> | ||||
1109 | |||||
1110 | Returns a scalar with the mask as an IPv4 or IPv6 text string as | ||||
1111 | described above. | ||||
1112 | |||||
1113 | =cut | ||||
1114 | |||||
1115 | sub mask ($) { | ||||
1116 | return ipv6_n2x($_[0]->{mask}) if $_[0]->{isv6}; | ||||
1117 | my $mask = isIPv4($_[0]->{addr}) | ||||
1118 | ? $_[0]->{mask} & V4net | ||||
1119 | : $_[0]->{mask}; | ||||
1120 | return inet_n2dx($mask); | ||||
1121 | } | ||||
1122 | |||||
1123 | =item C<-E<gt>masklen()> | ||||
1124 | |||||
1125 | Returns a scalar the number of one bits in the mask. | ||||
1126 | |||||
1127 | =cut | ||||
1128 | |||||
1129 | # spent 140µs (115+25) within NetAddr::IP::Lite::masklen which was called 4 times, avg 35µs/call:
# 4 times (115µs+25µs) by NetAddr::IP::Lite::cidr at line 1169, avg 35µs/call | ||||
1130 | 4 | 53µs | 4 | 12µs | my $len = (notcontiguous($_[0]->{mask}))[1]; # spent 12µs making 4 calls to NetAddr::IP::Util::notcontiguous, avg 3µs/call |
1131 | 4 | 7µs | return 0 unless $len; | ||
1132 | 4 | 39µs | return $len if $_[0]->{isv6}; | ||
1133 | return isIPv4($_[0]->{addr}) | ||||
1134 | 2 | 36µs | 2 | 13µs | ? $len -96 # spent 13µs making 2 calls to NetAddr::IP::InetBase::isIPv4, avg 6µs/call |
1135 | : $len; | ||||
1136 | } | ||||
1137 | |||||
1138 | =item C<-E<gt>bits()> | ||||
1139 | |||||
1140 | Returns the width of the address in bits. Normally 32 for v4 and 128 for v6. | ||||
1141 | |||||
1142 | =cut | ||||
1143 | |||||
1144 | sub bits { | ||||
1145 | return $_[0]->{isv6} ? 128 : 32; | ||||
1146 | } | ||||
1147 | |||||
1148 | =item C<-E<gt>version()> | ||||
1149 | |||||
1150 | Returns the version of the address or subnet. Currently this can be | ||||
1151 | either 4 or 6. | ||||
1152 | |||||
1153 | =cut | ||||
1154 | |||||
1155 | sub version { | ||||
1156 | my $self = shift; | ||||
1157 | return $self->{isv6} ? 6 : 4; | ||||
1158 | } | ||||
1159 | |||||
1160 | =item C<-E<gt>cidr()> | ||||
1161 | |||||
1162 | Returns a scalar with the address and mask in CIDR notation. A | ||||
1163 | NetAddr::IP::Lite object I<stringifies> to the result of this function. | ||||
1164 | (see comments about ->new6() and ->addr() for output formats) | ||||
1165 | |||||
1166 | =cut | ||||
1167 | |||||
1168 | # spent 1.54ms (113µs+1.43) within NetAddr::IP::Lite::cidr which was called 4 times, avg 385µs/call:
# 4 times (113µs+1.43ms) by NetAddr::IP::Lite::__ANON__[/usr/local/lib/perl5/site_perl/mach/5.24/NetAddr/IP/Lite.pm:238] at line 238, avg 385µs/call | ||||
1169 | 4 | 102µs | 8 | 1.43ms | return $_[0]->addr . '/' . $_[0]->masklen; # spent 1.29ms making 4 calls to NetAddr::IP::Lite::addr, avg 321µs/call
# spent 140µs making 4 calls to NetAddr::IP::Lite::masklen, avg 35µs/call |
1170 | } | ||||
1171 | |||||
1172 | =item C<-E<gt>aton()> | ||||
1173 | |||||
1174 | Returns the address part of the NetAddr::IP::Lite object in the same format | ||||
1175 | as the C<inet_aton()> or C<ipv6_aton> function respectively. If the object | ||||
1176 | was created using ->new6($ip), the address returned will always be in ipV6 | ||||
1177 | format, even for addresses in ipV4 address space. | ||||
1178 | |||||
1179 | =cut | ||||
1180 | |||||
1181 | sub aton { | ||||
1182 | return $_[0]->{addr} if $_[0]->{isv6}; | ||||
1183 | return isIPv4($_[0]->{addr}) | ||||
1184 | ? ipv6to4($_[0]->{addr}) | ||||
1185 | : $_[0]->{addr}; | ||||
1186 | } | ||||
1187 | |||||
1188 | =item C<-E<gt>range()> | ||||
1189 | |||||
1190 | Returns a scalar with the base address and the broadcast address | ||||
1191 | separated by a dash and spaces. This is called range notation. | ||||
1192 | |||||
1193 | =cut | ||||
1194 | |||||
1195 | sub range ($) { | ||||
1196 | return $_[0]->network->addr . ' - ' . $_[0]->broadcast->addr; | ||||
1197 | } | ||||
1198 | |||||
1199 | =item C<-E<gt>numeric()> | ||||
1200 | |||||
1201 | When called in a scalar context, will return a numeric representation | ||||
1202 | of the address part of the IP address. When called in an array | ||||
1203 | context, it returns a list of two elements. The first element is as | ||||
1204 | described, the second element is the numeric representation of the | ||||
1205 | netmask. | ||||
1206 | |||||
1207 | This method is essential for serializing the representation of a | ||||
1208 | subnet. | ||||
1209 | |||||
1210 | =cut | ||||
1211 | |||||
1212 | sub numeric ($) { | ||||
1213 | if (wantarray) { | ||||
1214 | if (! $_[0]->{isv6} && isIPv4($_[0]->{addr})) { | ||||
1215 | return ( sprintf("%u",unpack('N',ipv6to4($_[0]->{addr}))), | ||||
1216 | sprintf("%u",unpack('N',ipv6to4($_[0]->{mask})))); | ||||
1217 | } | ||||
1218 | else { | ||||
1219 | return ( bin2bcd($_[0]->{addr}), | ||||
1220 | bin2bcd($_[0]->{mask})); | ||||
1221 | } | ||||
1222 | } | ||||
1223 | return (! $_[0]->{isv6} && isIPv4($_[0]->{addr})) | ||||
1224 | ? sprintf("%u",unpack('N',ipv6to4($_[0]->{addr}))) | ||||
1225 | : bin2bcd($_[0]->{addr}); | ||||
1226 | } | ||||
1227 | |||||
1228 | =item C<-E<gt>bigint()> | ||||
1229 | |||||
1230 | When called in a scalar context, will return a Math::BigInt representation | ||||
1231 | of the address part of the IP address. When called in an array | ||||
1232 | contest, it returns a list of two elements. The first element is as | ||||
1233 | described, the second element is the Math::BigInt representation of the | ||||
1234 | netmask. | ||||
1235 | |||||
1236 | =cut | ||||
1237 | |||||
1238 | 1 | 2µs | my $biloaded; | ||
1239 | my $bi2strng; | ||||
1240 | 1 | 2µs | my $no_mbi_emu = 1; | ||
1241 | |||||
1242 | # function to force into test development mode | ||||
1243 | # | ||||
1244 | sub _force_bi_emu { | ||||
1245 | undef $biloaded; | ||||
1246 | undef $bi2strng; | ||||
1247 | $no_mbi_emu = 0; | ||||
1248 | print STDERR "\n\n\tWARNING: test development mode, this | ||||
1249 | \tmessage SHOULD NEVER BE SEEN IN PRODUCTION! | ||||
1250 | set my \$no_mbi_emu = 1 in t/bigint.t to remove this warning\n\n"; | ||||
1251 | } | ||||
1252 | |||||
1253 | # function to stringify various flavors of Math::BigInt objects | ||||
1254 | # tests to see if the object is a hash or a signed scalar | ||||
1255 | |||||
1256 | sub _bi_stfy { | ||||
1257 | "$_[0]" =~ /(\d+)/; # stringify and remove '+' if present | ||||
1258 | $1; | ||||
1259 | } | ||||
1260 | |||||
1261 | sub _fakebi2strg { | ||||
1262 | ${$_[0]} =~ /(\d+)/; | ||||
1263 | $1; | ||||
1264 | } | ||||
1265 | |||||
1266 | # fake new from bi string Math::BigInt 0.01 | ||||
1267 | # | ||||
1268 | sub _bi_fake { | ||||
1269 | bless \('+'. $_[1]), 'Math::BigInt'; | ||||
1270 | } | ||||
1271 | |||||
1272 | # as of this writing there are three known flavors of Math::BigInt | ||||
1273 | # v0.01 MBI::new returns a scalar ref | ||||
1274 | # v1.?? - 1.69 CALC::_new takes a reference to a scalar, returns an array, MBI returns a hash ref | ||||
1275 | # v1.70 and up CALC::_new takes a scalar, returns and array, MBI returns a hash ref | ||||
1276 | |||||
1277 | sub _loadMBI { # load Math::BigInt on demand | ||||
1278 | if (eval {$no_mbi_emu && require Math::BigInt}) { # any version should work, three known | ||||
1279 | import Math::BigInt; | ||||
1280 | $biloaded = \&Math::BigInt::new; | ||||
1281 | $bi2strng = \&_bi_stfy; | ||||
1282 | } else { | ||||
1283 | $biloaded = \&_bi_fake; | ||||
1284 | $bi2strng = \&_fakebi2strg; | ||||
1285 | } | ||||
1286 | } | ||||
1287 | |||||
1288 | sub _retMBIstring { | ||||
1289 | _loadMBI unless $biloaded; # load Math::BigInt on demand | ||||
1290 | $bi2strng->(@_); | ||||
1291 | } | ||||
1292 | |||||
1293 | sub _biRef { | ||||
1294 | _loadMBI unless $biloaded; # load Math::BigInt on demand | ||||
1295 | $biloaded->('Math::BigInt',$_[0]); | ||||
1296 | } | ||||
1297 | |||||
1298 | sub bigint($) { | ||||
1299 | my($addr,$mask); | ||||
1300 | if (wantarray) { | ||||
1301 | if (! $_[0]->{isv6} && isIPv4($_[0]->{addr})) { | ||||
1302 | $addr = $_[0]->{addr} | ||||
1303 | ? sprintf("%u",unpack('N',ipv6to4($_[0]->{addr}))) | ||||
1304 | : 0; | ||||
1305 | $mask = $_[0]->{mask} | ||||
1306 | ? sprintf("%u",unpack('N',ipv6to4($_[0]->{mask}))) | ||||
1307 | : 0; | ||||
1308 | } | ||||
1309 | else { | ||||
1310 | $addr = $_[0]->{addr} | ||||
1311 | ? bin2bcd($_[0]->{addr}) | ||||
1312 | : 0; | ||||
1313 | $mask = $_[0]->{mask} | ||||
1314 | ? bin2bcd($_[0]->{mask}) | ||||
1315 | : 0; | ||||
1316 | } | ||||
1317 | (_biRef($addr),_biRef($mask)); | ||||
1318 | |||||
1319 | } else { # not wantarray | ||||
1320 | |||||
1321 | if (! $_[0]->{isv6} && isIPv4($_[0]->{addr})) { | ||||
1322 | $addr = $_[0]->{addr} | ||||
1323 | ? sprintf("%u",unpack('N',ipv6to4($_[0]->{addr}))) | ||||
1324 | : 0; | ||||
1325 | } else { | ||||
1326 | $addr = $_[0]->{addr} | ||||
1327 | ? bin2bcd($_[0]->{addr}) | ||||
1328 | : 0; | ||||
1329 | } | ||||
1330 | _biRef($addr); | ||||
1331 | } | ||||
1332 | } | ||||
1333 | |||||
1334 | =item C<$me-E<gt>contains($other)> | ||||
1335 | |||||
1336 | Returns true when C<$me> completely contains C<$other>. False is | ||||
1337 | returned otherwise and C<undef> is returned if C<$me> and C<$other> | ||||
1338 | are not both C<NetAddr::IP::Lite> objects. | ||||
1339 | |||||
1340 | =cut | ||||
1341 | |||||
1342 | # spent 144µs (25+119) within NetAddr::IP::Lite::contains which was called 2 times, avg 72µs/call:
# 2 times (25µs+119µs) by Mail::SpamAssassin::NetSet::_nets_contains_network at line 229 of Mail/SpamAssassin/NetSet.pm, avg 72µs/call | ||||
1343 | 2 | 24µs | 2 | 119µs | return within(@_[1,0]); # spent 119µs making 2 calls to NetAddr::IP::Lite::within, avg 59µs/call |
1344 | } | ||||
1345 | |||||
1346 | =item C<$me-E<gt>within($other)> | ||||
1347 | |||||
1348 | The complement of C<-E<gt>contains()>. Returns true when C<$me> is | ||||
1349 | completely contained within C<$other>, undef if C<$me> and C<$other> | ||||
1350 | are not both C<NetAddr::IP::Lite> objects. | ||||
1351 | |||||
1352 | =cut | ||||
1353 | |||||
1354 | # spent 119µs (89+29) within NetAddr::IP::Lite::within which was called 2 times, avg 59µs/call:
# 2 times (89µs+29µs) by NetAddr::IP::Lite::contains at line 1343, avg 59µs/call | ||||
1355 | 2 | 46µs | 2 | 22µs | return 1 unless hasbits($_[1]->{mask}); # 0x0 contains everything # spent 22µs making 2 calls to NetAddr::IP::Util::hasbits, avg 11µs/call |
1356 | 2 | 8µs | my $netme = $_[0]->{addr} & $_[0]->{mask}; | ||
1357 | 2 | 10µs | my $brdme = $_[0]->{addr} | ~ $_[0]->{mask}; | ||
1358 | 2 | 7µs | my $neto = $_[1]->{addr} & $_[1]->{mask}; | ||
1359 | 2 | 7µs | my $brdo = $_[1]->{addr} | ~ $_[1]->{mask}; | ||
1360 | 2 | 44µs | 2 | 7µs | return (sub128($netme,$neto) && sub128($brdo,$brdme)) # spent 7µs making 2 calls to NetAddr::IP::Util::sub128, avg 4µs/call |
1361 | ? 1 : 0; | ||||
1362 | } | ||||
1363 | |||||
1364 | =item C-E<gt>is_rfc1918()> | ||||
1365 | |||||
1366 | Returns true when C<$me> is an RFC 1918 address. | ||||
1367 | |||||
1368 | 10.0.0.0 - 10.255.255.255 (10/8 prefix) | ||||
1369 | 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) | ||||
1370 | 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) | ||||
1371 | |||||
1372 | =cut | ||||
1373 | |||||
1374 | 1 | 11µs | 1 | 11µs | my $ip_10 = NetAddr::IP::Lite->new('10.0.0.0/8'); # spent 11µs making 1 call to NetAddr::IP::Lite::new |
1375 | 1 | 64µs | my $ip_10n = $ip_10->{addr}; # already the right value | ||
1376 | 1 | 7µs | my $ip_10b = $ip_10n | ~ $ip_10->{mask}; | ||
1377 | |||||
1378 | 1 | 7µs | 1 | 7µs | my $ip_172 = NetAddr::IP::Lite->new('172.16.0.0/12'); # spent 7µs making 1 call to NetAddr::IP::Lite::new |
1379 | 1 | 2µs | my $ip_172n = $ip_172->{addr}; # already the right value | ||
1380 | 1 | 4µs | my $ip_172b = $ip_172n | ~ $ip_172->{mask}; | ||
1381 | |||||
1382 | 1 | 6µs | 1 | 7µs | my $ip_192 = NetAddr::IP::Lite->new('192.168.0.0/16'); # spent 7µs making 1 call to NetAddr::IP::Lite::new |
1383 | 1 | 3µs | my $ip_192n = $ip_192->{addr}; # already the right value | ||
1384 | 1 | 10µs | my $ip_192b = $ip_192n | ~ $ip_192->{mask}; | ||
1385 | |||||
1386 | sub is_rfc1918 ($) { | ||||
1387 | my $netme = $_[0]->{addr} & $_[0]->{mask}; | ||||
1388 | my $brdme = $_[0]->{addr} | ~ $_[0]->{mask}; | ||||
1389 | return 1 if (sub128($netme,$ip_10n) && sub128($ip_10b,$brdme)); | ||||
1390 | return 1 if (sub128($netme,$ip_192n) && sub128($ip_192b,$brdme)); | ||||
1391 | return (sub128($netme,$ip_172n) && sub128($ip_172b,$brdme)) | ||||
1392 | ? 1 : 0; | ||||
1393 | } | ||||
1394 | |||||
1395 | =item C<-E<gt>is_local()> | ||||
1396 | |||||
1397 | Returns true when C<$me> is a local network address. | ||||
1398 | |||||
1399 | i.e. ipV4 127.0.0.0 - 127.255.255.255 | ||||
1400 | or ipV6 === ::1 | ||||
1401 | |||||
1402 | =cut | ||||
1403 | |||||
1404 | 1 | 6µs | 1 | 7µs | my $_lclhost6 = NetAddr::IP::Lite->new('::1'); # spent 7µs making 1 call to NetAddr::IP::Lite::new |
1405 | 1 | 6µs | 1 | 7µs | my $_lclnet = NetAddr::IP::Lite->new('127/8'); # spent 7µs making 1 call to NetAddr::IP::Lite::new |
1406 | |||||
1407 | sub is_local ($) { | ||||
1408 | return ($_[0]->{isv6}) | ||||
1409 | ? $_[0] == $_lclhost6 | ||||
1410 | : $_[0]->within($_lclnet); | ||||
1411 | } | ||||
1412 | |||||
1413 | =item C<-E<gt>first()> | ||||
1414 | |||||
1415 | Returns a new object representing the first usable IP address within | ||||
1416 | the subnet (ie, the first host address). | ||||
1417 | |||||
1418 | =cut | ||||
1419 | |||||
1420 | 1 | 2µs | 1 | 12µs | my $_cidr127 = pack('N4',0xffffffff,0xffffffff,0xffffffff,0xfffffffe); # spent 12µs making 1 call to main::CORE:pack |
1421 | |||||
1422 | sub first ($) { | ||||
1423 | if (hasbits($_[0]->{mask} ^ $_cidr127)) { | ||||
1424 | return $_[0]->network + 1; | ||||
1425 | } else { | ||||
1426 | return $_[0]->network; | ||||
1427 | } | ||||
1428 | # return $_[0]->network + 1; | ||||
1429 | } | ||||
1430 | |||||
1431 | =item C<-E<gt>last()> | ||||
1432 | |||||
1433 | Returns a new object representing the last usable IP address within | ||||
1434 | the subnet (ie, one less than the broadcast address). | ||||
1435 | |||||
1436 | =cut | ||||
1437 | |||||
1438 | sub last ($) { | ||||
1439 | if (hasbits($_[0]->{mask} ^ $_cidr127)) { | ||||
1440 | return $_[0]->broadcast - 1; | ||||
1441 | } else { | ||||
1442 | return $_[0]->broadcast; | ||||
1443 | } | ||||
1444 | # return $_[0]->broadcast - 1; | ||||
1445 | } | ||||
1446 | |||||
1447 | =item C<-E<gt>nth($index)> | ||||
1448 | |||||
1449 | Returns a new object representing the I<n>-th usable IP address within | ||||
1450 | the subnet (ie, the I<n>-th host address). If no address is available | ||||
1451 | (for example, when the network is too small for C<$index> hosts), | ||||
1452 | C<undef> is returned. | ||||
1453 | |||||
1454 | Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite implements | ||||
1455 | C<-E<gt>nth($index)> and C<-E<gt>num()> exactly as the documentation states. | ||||
1456 | Previous versions behaved slightly differently and not in a consistent | ||||
1457 | manner. | ||||
1458 | |||||
1459 | To use the old behavior for C<-E<gt>nth($index)> and C<-E<gt>num()>: | ||||
1460 | |||||
1461 | use NetAddr::IP::Lite qw(:old_nth); | ||||
1462 | |||||
1463 | old behavior: | ||||
1464 | NetAddr::IP->new('10/32')->nth(0) == undef | ||||
1465 | NetAddr::IP->new('10/32')->nth(1) == undef | ||||
1466 | NetAddr::IP->new('10/31')->nth(0) == undef | ||||
1467 | NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/31 | ||||
1468 | NetAddr::IP->new('10/30')->nth(0) == undef | ||||
1469 | NetAddr::IP->new('10/30')->nth(1) == 10.0.0.1/30 | ||||
1470 | NetAddr::IP->new('10/30')->nth(2) == 10.0.0.2/30 | ||||
1471 | NetAddr::IP->new('10/30')->nth(3) == 10.0.0.3/30 | ||||
1472 | |||||
1473 | Note that in each case, the broadcast address is represented in the | ||||
1474 | output set and that the 'zero'th index is alway undef except for | ||||
1475 | a point-to-point /31 or /127 network where there are exactly two | ||||
1476 | addresses in the network. | ||||
1477 | |||||
1478 | new behavior: | ||||
1479 | NetAddr::IP->new('10/32')->nth(0) == 10.0.0.0/32 | ||||
1480 | NetAddr::IP->new('10.1/32'->nth(0) == 10.0.0.1/32 | ||||
1481 | NetAddr::IP->new('10/31')->nth(0) == 10.0.0.0/32 | ||||
1482 | NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/32 | ||||
1483 | NetAddr::IP->new('10/30')->nth(0) == 10.0.0.1/30 | ||||
1484 | NetAddr::IP->new('10/30')->nth(1) == 10.0.0.2/30 | ||||
1485 | NetAddr::IP->new('10/30')->nth(2) == undef | ||||
1486 | |||||
1487 | Note that a /32 net always has 1 usable address while a /31 has exactly | ||||
1488 | two usable addresses for point-to-point addressing. The first | ||||
1489 | index (0) returns the address immediately following the network address | ||||
1490 | except for a /31 or /127 when it return the network address. | ||||
1491 | |||||
1492 | =cut | ||||
1493 | |||||
1494 | sub nth ($$) { | ||||
1495 | my $self = shift; | ||||
1496 | my $count = shift; | ||||
1497 | |||||
1498 | my $slash31 = ! hasbits($self->{mask} ^ $_cidr127); | ||||
1499 | if ($Old_nth) { | ||||
1500 | return undef if $slash31 && $count != 1; | ||||
1501 | return undef if ($count < 1 or $count > $self->num ()); | ||||
1502 | } | ||||
1503 | elsif ($slash31) { | ||||
1504 | return undef if ($count && $count != 1); # only index 0, 1 allowed for /31 | ||||
1505 | } else { | ||||
1506 | ++$count; | ||||
1507 | return undef if ($count < 1 or $count > $self->num ()); | ||||
1508 | } | ||||
1509 | return $self->network + $count; | ||||
1510 | } | ||||
1511 | |||||
1512 | =item C<-E<gt>num()> | ||||
1513 | |||||
1514 | As of version 4.42 of NetAddr::IP and version 1.27 of NetAddr::IP::Lite | ||||
1515 | a /31 and /127 with return a net B<num> value of 2 instead of 0 (zero) | ||||
1516 | for point-to-point networks. | ||||
1517 | |||||
1518 | Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite | ||||
1519 | return the number of usable IP addresses within the subnet, | ||||
1520 | not counting the broadcast or network address. | ||||
1521 | |||||
1522 | Previous versions worked only for ipV4 addresses, returned a | ||||
1523 | maximum span of 2**32 and returned the number of IP addresses | ||||
1524 | not counting the broadcast address. | ||||
1525 | (one greater than the new behavior) | ||||
1526 | |||||
1527 | To use the old behavior for C<-E<gt>nth($index)> and C<-E<gt>num()>: | ||||
1528 | |||||
1529 | use NetAddr::IP::Lite qw(:old_nth); | ||||
1530 | |||||
1531 | WARNING: | ||||
1532 | |||||
1533 | NetAddr::IP will calculate and return a numeric string for network | ||||
1534 | ranges as large as 2**128. These values are TEXT strings and perl | ||||
1535 | can treat them as integers for numeric calculations. | ||||
1536 | |||||
1537 | Perl on 32 bit platforms only handles integer numbers up to 2**32 | ||||
1538 | and on 64 bit platforms to 2**64. | ||||
1539 | |||||
1540 | If you wish to manipulate numeric strings returned by NetAddr::IP | ||||
1541 | that are larger than 2**32 or 2**64, respectively, you must load | ||||
1542 | additional modules such as Math::BigInt, bignum or some similar | ||||
1543 | package to do the integer math. | ||||
1544 | |||||
1545 | =cut | ||||
1546 | |||||
1547 | sub num ($) { | ||||
1548 | if ($Old_nth) { | ||||
1549 | my @net = unpack('L3N',$_[0]->{mask} ^ Ones); | ||||
1550 | # number of ip's less broadcast | ||||
1551 | return 0xfffffffe if $net[0] || $net[1] || $net[2]; # 2**32 -1 | ||||
1552 | return $net[3] if $net[3]; | ||||
1553 | } else { # returns 1 for /32 /128, 2 for /31 /127 else n-2 up to 2**32 | ||||
1554 | (undef, my $net) = addconst($_[0]->{mask},1); | ||||
1555 | return 1 unless hasbits($net); # ipV4/32 or ipV6/128 | ||||
1556 | $net = $net ^ Ones; | ||||
1557 | return 2 unless hasbits($net); # ipV4/31 or ipV6/127 | ||||
1558 | $net &= $_v4net unless $_[0]->{isv6}; | ||||
1559 | return bin2bcd($net); | ||||
1560 | } | ||||
1561 | } | ||||
1562 | |||||
1563 | # deprecated | ||||
1564 | #sub num ($) { | ||||
1565 | # my @net = unpack('L3N',$_[0]->{mask} ^ Ones); | ||||
1566 | # if ($Old_nth) { | ||||
1567 | ## number of ip's less broadcast | ||||
1568 | # return 0xfffffffe if $net[0] || $net[1] || $net[2]; # 2**32 -1 | ||||
1569 | # return $net[3] if $net[3]; | ||||
1570 | # } else { # returns 1 for /32 /128, 0 for /31 /127 else n-2 up to 2**32 | ||||
1571 | ## number of usable IP's === number of ip's less broadcast & network addys | ||||
1572 | # return 0xfffffffd if $net[0] || $net[1] || $net[2]; # 2**32 -2 | ||||
1573 | # return 1 unless $net[3]; | ||||
1574 | # $net[3]--; | ||||
1575 | # } | ||||
1576 | # return $net[3]; | ||||
1577 | #} | ||||
1578 | |||||
1579 | =pod | ||||
1580 | |||||
1581 | =back | ||||
1582 | |||||
1583 | =cut | ||||
1584 | |||||
1585 | # spent 832µs (214+617) within NetAddr::IP::Lite::import which was called:
# once (214µs+617µs) by NetAddr::IP::BEGIN@8 at line 8 of NetAddr/IP.pm | ||||
1586 | 7 | 48µs | if (grep { $_ eq ':aton' } @_) { | ||
1587 | $Accept_Binary_IP = 1; | ||||
1588 | @_ = grep { $_ ne ':aton' } @_; | ||||
1589 | } | ||||
1590 | 7 | 34µs | if (grep { $_ eq ':old_nth' } @_) { | ||
1591 | $Old_nth = 1; | ||||
1592 | @_ = grep { $_ ne ':old_nth' } @_; | ||||
1593 | } | ||||
1594 | 7 | 40µs | if (grep { $_ eq ':lower' } @_) | ||
1595 | { | ||||
1596 | NetAddr::IP::Util::lower(); | ||||
1597 | @_ = grep { $_ ne ':lower' } @_; | ||||
1598 | } | ||||
1599 | 7 | 32µs | if (grep { $_ eq ':upper' } @_) | ||
1600 | { | ||||
1601 | NetAddr::IP::Util::upper(); | ||||
1602 | @_ = grep { $_ ne ':upper' } @_; | ||||
1603 | } | ||||
1604 | 7 | 24µs | if (grep { $_ eq ':nofqdn' } @_) | ||
1605 | { | ||||
1606 | $NoFQDN = 1; | ||||
1607 | @_ = grep { $_ ne ':nofqdn' } @_; | ||||
1608 | } | ||||
1609 | 1 | 27µs | 1 | 68µs | NetAddr::IP::Lite->export_to_level(1, @_); # spent 68µs making 1 call to Exporter::export_to_level |
1610 | } | ||||
1611 | |||||
1612 | =head1 EXPORT_OK | ||||
1613 | |||||
1614 | Zeros | ||||
1615 | Ones | ||||
1616 | V4mask | ||||
1617 | V4net | ||||
1618 | :aton DEPRECATED | ||||
1619 | :old_nth | ||||
1620 | :upper | ||||
1621 | :lower | ||||
1622 | :nofqdn | ||||
1623 | |||||
1624 | =head1 AUTHORS | ||||
1625 | |||||
1626 | Luis E. Muñoz E<lt>luismunoz@cpan.orgE<gt>, | ||||
1627 | Michael Robinton E<lt>michael@bizsystems.comE<gt> | ||||
1628 | |||||
1629 | =head1 WARRANTY | ||||
1630 | |||||
1631 | This software comes with the same warranty as perl itself (ie, none), | ||||
1632 | so by using it you accept any and all the liability. | ||||
1633 | |||||
1634 | =head1 COPYRIGHT | ||||
1635 | |||||
1636 | This software is (c) Luis E. Muñoz, 1999 - 2005 | ||||
1637 | and (c) Michael Robinton, 2006 - 2014. | ||||
1638 | |||||
1639 | All rights reserved. | ||||
1640 | |||||
1641 | This program is free software; you can redistribute it and/or modify | ||||
1642 | it under the terms of either: | ||||
1643 | |||||
1644 | a) the GNU General Public License as published by the Free | ||||
1645 | Software Foundation; either version 2, or (at your option) any | ||||
1646 | later version, or | ||||
1647 | |||||
1648 | b) the "Artistic License" which comes with this distribution. | ||||
1649 | |||||
1650 | This program is distributed in the hope that it will be useful, | ||||
1651 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
1652 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either | ||||
1653 | the GNU General Public License or the Artistic License for more details. | ||||
1654 | |||||
1655 | You should have received a copy of the Artistic License with this | ||||
1656 | distribution, in the file named "Artistic". If not, I'll be glad to provide | ||||
1657 | one. | ||||
1658 | |||||
1659 | You should also have received a copy of the GNU General Public License | ||||
1660 | along with this program in the file named "Copying". If not, write to the | ||||
1661 | |||||
1662 | Free Software Foundation, Inc., | ||||
1663 | 51 Franklin Street, Fifth Floor | ||||
1664 | Boston, MA 02110-1301 USA | ||||
1665 | |||||
1666 | or visit their web page on the internet at: | ||||
1667 | |||||
1668 | http://www.gnu.org/copyleft/gpl.html. | ||||
1669 | |||||
1670 | =head1 SEE ALSO | ||||
1671 | |||||
1672 | NetAddr::IP(3), NetAddr::IP::Util(3), NetAddr::IP::InetBase(3) | ||||
1673 | |||||
1674 | =cut | ||||
1675 | |||||
1676 | 1 | 88µs | 1; | ||
# spent 520µs within NetAddr::IP::Lite::CORE:match which was called 117 times, avg 4µs/call:
# 47 times (251µs+0s) by NetAddr::IP::Lite::_xnew at line 781, avg 5µs/call
# 28 times (77µs+0s) by NetAddr::IP::Lite::_xnew at line 850, avg 3µs/call
# 19 times (90µs+0s) by NetAddr::IP::Lite::_xnew at line 834, avg 5µs/call
# 9 times (40µs+0s) by NetAddr::IP::Lite::_xnew at line 922, avg 4µs/call
# 9 times (22µs+0s) by NetAddr::IP::Lite::_xnew at line 1044, avg 2µs/call
# 4 times (16µs+0s) by NetAddr::IP::Lite::_xnew at line 855, avg 4µs/call
# once (23µs+0s) by NetAddr::IP::BEGIN@8 at line 35 |