← Index
NYTProf Performance Profile   « line view »
For /usr/local/bin/sa-learn
  Run on Sun Nov 5 03:09:29 2017
Reported on Mon Nov 6 13:20:46 2017

Filename/usr/local/lib/perl5/site_perl/mach/5.24/NetAddr/IP.pm
StatementsExecuted 85 statements in 3.80ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11114.0ms53.1msNetAddr::IP::::BEGIN@8NetAddr::IP::BEGIN@8
333413µs1.12msNetAddr::IP::::importNetAddr::IP::import
611170µs202µsNetAddr::IP::::full6NetAddr::IP::full6
11151µs64µsNetAddr::IP::::BEGIN@5NetAddr::IP::BEGIN@5
11138µs639µsNetAddr::IP::::BEGIN@9NetAddr::IP::BEGIN@9
11138µs275µsNetAddr::IP::::BEGIN@25NetAddr::IP::BEGIN@25
61133µs33µsNetAddr::IP::::CORE:unpackNetAddr::IP::CORE:unpack (opcode)
11125µs106µsNetAddr::IP::::BEGIN@221NetAddr::IP::BEGIN@221
11124µs571µsNetAddr::IP::::BEGIN@23NetAddr::IP::BEGIN@23
11123µs195µsNetAddr::IP::::BEGIN@7NetAddr::IP::BEGIN@7
11110µs10µsNetAddr::IP::::CORE:matchNetAddr::IP::CORE:match (opcode)
0000s0sNetAddr::IP::::CoalesceNetAddr::IP::Coalesce
0000s0sNetAddr::IP::::DESTROYNetAddr::IP::DESTROY
0000s0sNetAddr::IP::::__ANON__[:223]NetAddr::IP::__ANON__[:223]
0000s0sNetAddr::IP::::__ANON__[:365]NetAddr::IP::__ANON__[:365]
0000s0sNetAddr::IP::::__ANON__[:377]NetAddr::IP::__ANON__[:377]
0000s0sNetAddr::IP::::compactNetAddr::IP::compact
0000s0sNetAddr::IP::::fullNetAddr::IP::full
0000s0sNetAddr::IP::::full6mNetAddr::IP::full6m
0000s0sNetAddr::IP::::hostenumrefNetAddr::IP::hostenumref
0000s0sNetAddr::IP::::netlimitNetAddr::IP::netlimit
0000s0sNetAddr::IP::::rsplitNetAddr::IP::rsplit
0000s0sNetAddr::IP::::rsplitrefNetAddr::IP::rsplitref
0000s0sNetAddr::IP::::splitNetAddr::IP::split
0000s0sNetAddr::IP::::splitrefNetAddr::IP::splitref
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1#!/usr/bin/perl -w
2
3package NetAddr::IP;
4
5265µs278µs
# spent 64µs (51+13) within NetAddr::IP::BEGIN@5 which was called: # once (51µs+13µs) by Mail::SpamAssassin::Util::BEGIN@76 at line 5
use strict;
# spent 64µs making 1 call to NetAddr::IP::BEGIN@5 # spent 13µs making 1 call to strict::import
6#use diagnostics;
7275µs2367µs
# spent 195µs (23+172) within NetAddr::IP::BEGIN@7 which was called: # once (23µs+172µs) by Mail::SpamAssassin::Util::BEGIN@76 at line 7
use Carp;
# spent 195µs making 1 call to NetAddr::IP::BEGIN@7 # spent 172µs making 1 call to Exporter::import
83457µs353.8ms
# spent 53.1ms (14.0+39.2) within NetAddr::IP::BEGIN@8 which was called: # once (14.0ms+39.2ms) by Mail::SpamAssassin::Util::BEGIN@76 at line 8
use NetAddr::IP::Lite 1.56 qw(Zero Zeros Ones V4mask V4net);
# spent 53.1ms making 1 call to NetAddr::IP::BEGIN@8 # spent 660µs making 1 call to NetAddr::IP::Lite::import # spent 29µs making 1 call to UNIVERSAL::VERSION
912µs
# spent 639µs (38+601) within NetAddr::IP::BEGIN@9 which was called: # once (38µs+601µs) by Mail::SpamAssassin::Util::BEGIN@76 at line 21
use NetAddr::IP::Util 1.53 qw(
10 sub128
11 inet_aton
12 inet_any2n
13 ipv6_aton
14 isIPv4
15 ipv4to6
16 mask4to6
17 shiftleft
18 addconst
19 hasbits
20 notcontiguous
21299µs31.24ms);
# spent 639µs making 1 call to NetAddr::IP::BEGIN@9 # spent 586µs making 1 call to NetAddr::IP::Util::import # spent 15µs making 1 call to UNIVERSAL::VERSION
22
23284µs21.12ms
# spent 571µs (24+548) within NetAddr::IP::BEGIN@23 which was called: # once (24µs+548µs) by Mail::SpamAssassin::Util::BEGIN@76 at line 23
use AutoLoader qw(AUTOLOAD);
# spent 571µs making 1 call to NetAddr::IP::BEGIN@23 # spent 548µs making 1 call to AutoLoader::import
24
2512µs
# spent 275µs (38+238) within NetAddr::IP::BEGIN@25 which was called: # once (38µs+238µs) by Mail::SpamAssassin::Util::BEGIN@76 at line 32
use vars qw(
26 @EXPORT_OK
27 @EXPORT_FAIL
28 @ISA
29 $VERSION
30 $_netlimit
31 $rfc3021
321557µs2513µs);
# spent 275µs making 1 call to NetAddr::IP::BEGIN@25 # spent 238µs making 1 call to vars::import
3312µsrequire Exporter;
34
3515µs@EXPORT_OK = qw(Compact Coalesce Zero Zeros Ones V4mask V4net netlimit);
3612µs@EXPORT_FAIL = qw($_netlimit);
37
38117µs@ISA = qw(Exporter NetAddr::IP::Lite);
39
40234µs110µs$VERSION = do { sprintf " %d.%03d", (q$Revision: 4.78 $ =~ /\d+/g) };
# spent 10µs making 1 call to NetAddr::IP::CORE:match
41
4212µs$rfc3021 = 0;
43
44=pod
45
46=encoding UTF-8
47
48=head1 NAME
49
50NetAddr::IP - Manages IPv4 and IPv6 addresses and subnets
51
52=head1 SYNOPSIS
53
54 use NetAddr::IP qw(
55 Compact
56 Coalesce
57 Zeros
58 Ones
59 V4mask
60 V4net
61 netlimit
62 :aton DEPRECATED
63 :lower
64 :upper
65 :old_storable
66 :old_nth
67 :rfc3021
68 :nofqdn
69 );
70
71 NOTE: NetAddr::IP::Util has a full complement of network address
72 utilities to convert back and forth between binary and text.
73
74 inet_aton, inet_ntoa, ipv6_aton, ipv6_ntoa
75 ipv6_n2x, ipv6_n2d inet_any2d, inet_n2dx,
76 inet_n2ad, inetanyto6, ipv6to4
77
78See L<NetAddr::IP::Util>
79
80
81 my $ip = new NetAddr::IP '127.0.0.1';
82 or if you prefer
83 my $ip = NetAddr::IP->new('127.0.0.1);
84 or from a packed IPv4 address
85 my $ip = new_from_aton NetAddr::IP (inet_aton('127.0.0.1'));
86 or from an octal filtered IPv4 address
87 my $ip = new_no NetAddr::IP '127.012.0.0';
88
89 print "The address is ", $ip->addr, " with mask ", $ip->mask, "\n" ;
90
91 if ($ip->within(new NetAddr::IP "127.0.0.0", "255.0.0.0")) {
92 print "Is a loopback address\n";
93 }
94
95 # This prints 127.0.0.1/32
96 print "You can also say $ip...\n";
97
98* The following four functions return ipV6 representations of:
99
100 :: = Zeros();
101 FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF = Ones();
102 FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:: = V4mask();
103 ::FFFF:FFFF = V4net();
104
105 Will also return an ipV4 or ipV6 representation of a
106 resolvable Fully Qualified Domanin Name (FQDN).
107
108###### DEPRECATED, will be remove in version 5 ############
109
110 * To accept addresses in the format as returned by
111 inet_aton, invoke the module as:
112
113 use NetAddr::IP qw(:aton);
114
115###### USE new_from_aton instead ##########################
116
117* To enable usage of legacy data files containing NetAddr::IP
118objects stored using the L<Storable> module.
119
120 use NetAddr::IP qw(:old_storable);
121
122* To compact many smaller subnets (see: C<$me-E<gt>compact($addr1,$addr2,...)>
123
124 @compacted_object_list = Compact(@object_list)
125
126* Return a reference to list of C<NetAddr::IP> subnets of
127C<$masklen> mask length, when C<$number> or more addresses from
128C<@list_of_subnets> are found to be contained in said subnet.
129
130 $arrayref = Coalesce($masklen, $number, @list_of_subnets)
131
132* By default B<NetAddr::IP> functions and methods return string IPv6
133addresses in uppercase. To change that to lowercase:
134
135NOTE: the AUGUST 2010 RFC5952 states:
136
137 4.3. Lowercase
138
139 The characters "a", "b", "c", "d", "e", and "f" in an IPv6
140 address MUST be represented in lowercase.
141
142It is recommended that all NEW applications using NetAddr::IP be
143invoked as shown on the next line.
144
145 use NetAddr::IP qw(:lower);
146
147* To ensure the current IPv6 string case behavior even if the default changes:
148
149 use NetAddr::IP qw(:upper);
150
151* To set a limit on the size of B<nets> processed or returned by NetAddr::IP.
152
153Set the maximum number of nets beyond which NetAddr::IP will return
154an error as a power of 2 (default 16 or 65536 nets). Each 2**16
155consumes approximately 4 megs of memory. A 2**20 consumes 64 megs of
156memory, A 2**24 consumes 1 gigabyte of memory.
157
158 use NetAddr::IP qw(netlimit);
159 netlimit 20;
160
161The maximum B<netlimit> allowed is 2**24. Attempts to set limits below
162the default of 16 or above the maximum of 24 are ignored.
163
164Returns true on success, otherwise C<undef>.
165
166=cut
167
16812µs$_netlimit = 2 ** 16; # default
169
170sub netlimit($) {
171 return undef unless $_[0];
172 return undef if $_[0] =~ /\D/;
173 return undef if $_[0] < 16;
174 return undef if $_[0] > 24;
175 $_netlimit = 2 ** $_[0];
176};
177
178=head1 INSTALLATION
179
180Un-tar the distribution in an appropriate directory and type:
181
182 perl Makefile.PL
183 make
184 make test
185 make install
186
187B<NetAddr::IP> depends on B<NetAddr::IP::Util> which installs by
188default with its primary functions compiled using Perl's XS extensions
189to build a C library. If you do not have a C complier available or
190would like the slower Pure Perl version for some other reason, then
191type:
192
193 perl Makefile.PL -noxs
194 make
195 make test
196 make install
197
198=head1 DESCRIPTION
199
200This module provides an object-oriented abstraction on top of IP
201addresses or IP subnets that allows for easy manipulations. Version
2024.xx of NetAddr::IP will work with older versions of Perl and is
203compatible with Math::BigInt.
204
205The internal representation of all IP objects is in 128 bit IPv6 notation.
206IPv4 and IPv6 objects may be freely mixed.
207
208=head2 Overloaded Operators
209
210Many operators have been overloaded, as described below:
211
212=cut
213
214 #############################################
215 # These are the overload methods, placed here
216 # for convenience.
217 #############################################
218
219use overload
220
221
# spent 106µs (25+80) within NetAddr::IP::BEGIN@221 which was called: # once (25µs+80µs) by Mail::SpamAssassin::Util::BEGIN@76 at line 223
'@{}' => sub {
222 return [ $_[0]->hostenum ];
22321.75ms2186µs };
# spent 106µs making 1 call to NetAddr::IP::BEGIN@221 # spent 80µs making 1 call to overload::import
224
225=pod
226
227=over
228
229=item B<Assignment (C<=>)>
230
231Has been optimized to copy one NetAddr::IP object to another very quickly.
232
233=item B<C<-E<gt>copy()>>
234
235The B<assignment (C<=>)> operation is only put in to operation when the
236copied object is further mutated by another overloaded operation. See
237L<overload> B<SPECIAL SYMBOLS FOR "use overload"> for details.
238
239B<C<-E<gt>copy()>> actually creates a new object when called.
240
241=item B<Stringification>
242
243An object can be used just as a string. For instance, the following code
244
245 my $ip = new NetAddr::IP '192.168.1.123';
246 print "$ip\n";
247
248Will print the string 192.168.1.123/32.
249
250=item B<Equality>
251
252You can test for equality with either C<eq> or C<==>. C<eq> allows
253comparison with arbitrary strings as well as NetAddr::IP objects. The
254following example:
255
256 if (NetAddr::IP->new('127.0.0.1','255.0.0.0') eq '127.0.0.1/8')
257 { print "Yes\n"; }
258
259will print out "Yes".
260
261Comparison with C<==> requires both operands to be NetAddr::IP objects.
262
263In both cases, a true value is returned if the CIDR representation of
264the operands is equal.
265
266=item B<Comparison via E<gt>, E<lt>, E<gt>=, E<lt>=, E<lt>=E<gt> and C<cmp>>
267
268Internally, all network objects are represented in 128 bit format.
269The numeric representation of the network is compared through the
270corresponding operation. Comparisons are tried first on the address portion
271of the object and if that is equal then the NUMERIC cidr portion of the
272masks are compared. This leads to the counterintuitive result that
273
274 /24 > /16
275
276Comparison should not be done on netaddr objects with different CIDR as
277this may produce indeterminate - unexpected results,
278rather the determination of which netblock is larger or smaller should be
279done by comparing
280
281 $ip1->masklen <=> $ip2->masklen
282
283=item B<Addition of a constant (C<+>)>
284
285Add a 32 bit signed constant to the address part of a NetAddr object.
286This operation changes the address part to point so many hosts above the
287current objects start address. For instance, this code:
288
289 print NetAddr::IP->new('127.0.0.1/8') + 5;
290
291will output 127.0.0.6/8. The address will wrap around at the broadcast
292back to the network address. This code:
293
294 print NetAddr::IP->new('10.0.0.1/24') + 255;
295
296 outputs 10.0.0.0/24.
297
298Returns the the unchanged object when the constant is missing or out of
299range.
300
301 2147483647 <= constant >= -2147483648
302
303=item B<Subtraction of a constant (C<->)>
304
305The complement of the addition of a constant.
306
307=item B<Difference (C<->)>
308
309Returns the difference between the address parts of two NetAddr::IP
310objects address parts as a 32 bit signed number.
311
312Returns B<undef> if the difference is out of range.
313
314(See range restrictions on Addition above)
315
316=item B<Auto-increment>
317
318Auto-incrementing a NetAddr::IP object causes the address part to be
319adjusted to the next host address within the subnet. It will wrap at
320the broadcast address and start again from the network address.
321
322=item B<Auto-decrement>
323
324Auto-decrementing a NetAddr::IP object performs exactly the opposite
325of auto-incrementing it, as you would expect.
326
327=cut
328
329 #############################################
330 # End of the overload methods.
331 #############################################
332
333
334# Preloaded methods go here.
335
336=pod
337
338=back
339
340=head2 Serializing and Deserializing
341
342This module defines hooks to collaborate with L<Storable> for
343serializing C<NetAddr::IP> objects, through compact and human readable
344strings. You can revert to the old format by invoking this module as
345
346 use NetAddr::IP ':old_storable';
347
348You must do this if you have legacy data files containing NetAddr::IP
349objects stored using the L<Storable> module.
350
351=cut
352
35312µsmy $full_format = "%04X:%04X:%04X:%04X:%04X:%04X:%D.%D.%D.%D";
35412µsmy $full6_format = "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X";
355
356sub import
357
# spent 1.12ms (413µs+706µs) within NetAddr::IP::import which was called 3 times, avg 373µs/call: # once (156µs+274µs) by Mail::SpamAssassin::Plugin::TxRep::BEGIN@207 at line 207 of Mail/SpamAssassin/Plugin/TxRep.pm # once (184µs+193µs) by Mail::SpamAssassin::NetSet::BEGIN@26 at line 26 of Mail/SpamAssassin/NetSet.pm # once (73µs+239µs) by Mail::SpamAssassin::Util::BEGIN@76 at line 76 of Mail/SpamAssassin/Util.pm
{
358635µs if (grep { $_ eq ':old_storable' } @_) {
359 @_ = grep { $_ ne ':old_storable' } @_;
360 } else {
361 *{STORABLE_freeze} = sub
362 {
363 my $self = shift;
364 return $self->cidr(); # use stringification
3653147µs };
366 *{STORABLE_thaw} = sub
367 {
368 my $self = shift;
369 my $cloning = shift; # Not used
370 my $serial = shift;
371
372 my $ip = new NetAddr::IP $serial;
373 $self->{addr} = $ip->{addr};
374 $self->{mask} = $ip->{mask};
375 $self->{isv6} = $ip->{isv6};
376 return;
377358µs };
378 }
379
380623µs if (grep { $_ eq ':aton' } @_)
381 {
382 $NetAddr::IP::Lite::Accept_Binary_IP = 1;
383 @_ = grep { $_ ne ':aton' } @_;
384 }
385622µs if (grep { $_ eq ':old_nth' } @_)
386 {
387 $NetAddr::IP::Lite::Old_nth = 1;
388 @_ = grep { $_ ne ':old_nth' } @_;
389 }
390620µs if (grep { $_ eq ':lower' } @_)
391 {
392 $full_format = lc($full_format);
393 $full6_format = lc($full6_format);
394 NetAddr::IP::Util::lower();
395 @_ = grep { $_ ne ':lower' } @_;
396 }
397621µs if (grep { $_ eq ':upper' } @_)
398 {
399 $full_format = uc($full_format);
400 $full6_format = uc($full6_format);
401 NetAddr::IP::Util::upper();
402 @_ = grep { $_ ne ':upper' } @_;
403 }
404621µs if (grep { $_ eq ':rfc3021' } @_)
405 {
406 $rfc3021 = 1;
407 @_ = grep { $_ ne ':rfc3021' } @_;
408 }
409346µs3173µs NetAddr::IP->export_to_level(1, @_);
# spent 173µs making 3 calls to Exporter::export_to_level, avg 58µs/call
410}
411
412sub compact {
413 return (ref $_[0] eq 'ARRAY')
414 ? compactref($_[0]) # Compact(\@list)
415 : @{compactref(\@_)}; # Compact(@list) or ->compact(@list)
416}
417
41818µs*Compact = \&compact;
419
420sub Coalesce {
421 return &coalesce;
422}
423
424sub hostenumref($) {
425 my $r = _splitref(0,$_[0]);
426 unless ((notcontiguous($_[0]->{mask}))[1] == 128 ||
427 ($rfc3021 && $_[0]->masklen == 31) ) {
428 splice(@$r, 0, 1);
429 splice(@$r, scalar @$r - 1, 1);
430 }
431 return $r;
432}
433
434sub splitref {
435 unshift @_, 0; # mark as no reverse
436# perl 5.8.4 fails with this operation. see perl bug [ 23429]
437# goto &_splitref;
438 &_splitref;
439}
440
441sub rsplitref {
442 unshift @_, 1; # mark as reversed
443# perl 5.8.4 fails with this operation. see perl bug [ 23429]
444# goto &_splitref;
445 &_splitref;
446}
447
448sub split {
449 unshift @_, 0; # mark as no reverse
450 my $rv = &_splitref;
451 return $rv ? @$rv : ();
452}
453
454sub rsplit {
455 unshift @_, 1; # mark as reversed
456 my $rv = &_splitref;
457 return $rv ? @$rv : ();
458}
459
460sub full($) {
461 if (! $_[0]->{isv6} && isIPv4($_[0]->{addr})) {
462 my @hex = (unpack("n8",$_[0]->{addr}));
463 $hex[9] = $hex[7] & 0xff;
464 $hex[8] = $hex[7] >> 8;
465 $hex[7] = $hex[6] & 0xff;
466 $hex[6] >>= 8;
467 return sprintf($full_format,@hex);
468 } else {
469 &full6;
470 }
471}
472
473
# spent 202µs (170+33) within NetAddr::IP::full6 which was called 6 times, avg 34µs/call: # 6 times (170µs+33µs) by Mail::SpamAssassin::Util::reverse_ip_address at line 917 of Mail/SpamAssassin/Util.pm, avg 34µs/call
sub full6($) {
4746109µs633µs my @hex = (unpack("n8",$_[0]->{addr}));
# spent 33µs making 6 calls to NetAddr::IP::CORE:unpack, avg 5µs/call
4756111µs return sprintf($full6_format,@hex);
476}
477
478sub full6m($) {
479 my @hex = (unpack("n8",$_[0]->{mask}));
480 return sprintf($full6_format,@hex);
481}
482
483sub DESTROY {};
484
485118µs1;
486__END__
 
# spent 10µs within NetAddr::IP::CORE:match which was called: # once (10µs+0s) by Mail::SpamAssassin::Util::BEGIN@76 at line 40
sub NetAddr::IP::CORE:match; # opcode
# spent 33µs within NetAddr::IP::CORE:unpack which was called 6 times, avg 5µs/call: # 6 times (33µs+0s) by NetAddr::IP::full6 at line 474, avg 5µs/call
sub NetAddr::IP::CORE:unpack; # opcode