line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package NetAddr::IP::Count; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
685
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION @ISA $cache_num); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
69
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
94
|
|
6
|
1
|
|
|
1
|
|
1346
|
use NetAddr::IP; |
|
1
|
|
|
|
|
53556
|
|
|
1
|
|
|
|
|
6
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
@ISA = qw(); |
11
|
|
|
|
|
|
|
$VERSION = '2.01'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Preloaded methods go here. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
0
|
|
|
0
|
1
|
|
my $type = shift; |
17
|
0
|
|
0
|
|
|
|
my $class = ref($type) || $type || "NetAddr::IP::Count"; |
18
|
0
|
|
|
|
|
|
my $self = { 'subnets' => {}, |
19
|
|
|
|
|
|
|
'count' => {}, |
20
|
|
|
|
|
|
|
'cache' => [], |
21
|
|
|
|
|
|
|
'cache_num' => 5, |
22
|
|
|
|
|
|
|
}; |
23
|
0
|
|
|
|
|
|
bless $self, $class; |
24
|
0
|
|
|
|
|
|
my %data = @_; |
25
|
0
|
|
|
|
|
|
foreach my $subnet (keys %data) { |
26
|
0
|
|
|
|
|
|
$self->add($subnet, $data{$subnet}); |
27
|
|
|
|
|
|
|
} |
28
|
0
|
|
|
|
|
|
return $self; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub cache { |
32
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
33
|
0
|
|
|
|
|
|
my $ret = $self->{'cache_num'}; |
34
|
0
|
0
|
0
|
|
|
|
$self->{'cache_num'} = shift if (@_ and $_[0] >= 0); |
35
|
0
|
|
|
|
|
|
$ret; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub add { |
39
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
40
|
0
|
|
|
|
|
|
my $subnet = shift; |
41
|
0
|
0
|
|
|
|
|
if (ref($_[0]) eq 'ARRAY') { |
42
|
0
|
|
|
|
|
|
$self->_add_entry($subnet, @{$_[0]}); |
|
0
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
else { |
45
|
0
|
|
|
|
|
|
$self->_add_entry($subnet, @_); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _add_entry { |
50
|
0
|
|
|
0
|
|
|
my $self = shift; |
51
|
0
|
|
|
|
|
|
my $name = shift; |
52
|
0
|
0
|
|
|
|
|
if (not exists $self->{'subnets'}->{$name}) { |
53
|
0
|
|
|
|
|
|
$self->{'subnets'}->{$name} = []; |
54
|
0
|
|
|
|
|
|
$self->{'count'}->{$name} = 0; |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
push @{$self->{'subnets'}->{$name}}, @_; |
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _add_cache { |
60
|
0
|
|
|
0
|
|
|
my $self = shift; |
61
|
0
|
|
|
|
|
|
my ($label, $ip) = (shift, shift); |
62
|
0
|
|
|
|
|
|
unshift @{$self->{'cache'}}, [$label, $ip]; |
|
0
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
while (@{$self->{'cache'}} > $self->{'cache_num'}) { |
|
0
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
pop @{$self->{'cache'}}; |
|
0
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub count { |
69
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
70
|
|
|
|
|
|
|
IP: |
71
|
0
|
|
|
|
|
|
while (my $ip = shift) { |
72
|
0
|
|
|
|
|
|
foreach my $r_pair (@{$self->{'cache'}}) { |
|
0
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my ($subnet, $ip_net) = @{$r_pair}; |
|
0
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
if ($ip_net->contains($ip)) { # Match |
75
|
0
|
|
|
|
|
|
++$self->{'count'}->{$subnet}; |
76
|
0
|
|
|
|
|
|
next IP; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
|
foreach my $subnet (keys %{$self->{'subnets'}}) { |
|
0
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
foreach my $ip_net (@{$self->{'subnets'}->{$subnet}}) { |
|
0
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
if ($ip_net->contains($ip)) { # Match |
82
|
0
|
|
|
|
|
|
$self->_add_cache($subnet, $ip_net); |
83
|
0
|
|
|
|
|
|
++$self->{'count'}->{$subnet}; |
84
|
0
|
|
|
|
|
|
next IP; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub valcount { |
92
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
93
|
|
|
|
|
|
|
IP: |
94
|
0
|
|
|
|
|
|
while (my $ip = shift) { |
95
|
0
|
|
|
|
|
|
my $value = shift; |
96
|
0
|
|
|
|
|
|
foreach my $r_pair (@{$self->{'cache'}}) { |
|
0
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
my ($subnet, $ip_net) = @{$r_pair}; |
|
0
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
if ($ip_net->contains($ip)) { # Match |
99
|
0
|
|
|
|
|
|
$self->{'count'}->{$subnet} += $value; |
100
|
0
|
|
|
|
|
|
next IP; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
0
|
|
|
|
|
|
foreach my $subnet (keys %{$self->{'subnets'}}) { |
|
0
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
foreach my $ip_net (@{$self->{'subnets'}->{$subnet}}) { |
|
0
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
|
if ($ip_net->contains($ip)) { # Match |
106
|
0
|
|
|
|
|
|
$self->_add_cache($subnet, $ip_net); |
107
|
0
|
|
|
|
|
|
$self->{'count'}->{$subnet} += $value; |
108
|
0
|
|
|
|
|
|
next IP; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub result { |
116
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
117
|
0
|
|
|
|
|
|
my %res; |
118
|
0
|
|
|
|
|
|
foreach my $subnet (keys %{$self->{'count'}}) { |
|
0
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
$res{$subnet} = $self->{'count'}->{$subnet}; |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
|
return \%res; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
__END__ |