line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Zonemaster::Engine::ASNLookup; |
2
|
|
|
|
|
|
|
|
3
|
26
|
|
|
26
|
|
144
|
use version; our $VERSION = version->declare("v1.0.4"); |
|
26
|
|
|
|
|
53
|
|
|
26
|
|
|
|
|
163
|
|
4
|
|
|
|
|
|
|
|
5
|
26
|
|
|
26
|
|
2113
|
use 5.014002; |
|
26
|
|
|
|
|
101
|
|
6
|
26
|
|
|
26
|
|
123
|
use warnings; |
|
26
|
|
|
|
|
51
|
|
|
26
|
|
|
|
|
615
|
|
7
|
|
|
|
|
|
|
|
8
|
26
|
|
|
26
|
|
126
|
use Zonemaster::Engine::Net::IP; |
|
26
|
|
|
|
|
53
|
|
|
26
|
|
|
|
|
896
|
|
9
|
|
|
|
|
|
|
|
10
|
26
|
|
|
26
|
|
572
|
use Zonemaster::Engine; |
|
26
|
|
|
|
|
60
|
|
|
26
|
|
|
|
|
509
|
|
11
|
26
|
|
|
26
|
|
125
|
use Zonemaster::Engine::Nameserver; |
|
26
|
|
|
|
|
51
|
|
|
26
|
|
|
|
|
10049
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @roots; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub get_with_prefix { |
16
|
38
|
|
|
38
|
1
|
2002
|
my ( $class, $ip ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
38
|
100
|
|
|
|
104
|
if ( not @roots ) { |
19
|
3
|
|
|
|
|
9
|
@roots = map { Zonemaster::Engine->zone( $_ ) } @{ Zonemaster::Engine->config->asnroots }; |
|
9
|
|
|
|
|
32
|
|
|
3
|
|
|
|
|
13
|
|
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
38
|
100
|
66
|
|
|
243
|
if ( not ref( $ip ) or not $ip->isa( 'Zonemaster::Engine::Net::IP' ) ) { |
23
|
4
|
|
|
|
|
16
|
$ip = Zonemaster::Engine::Net::IP->new( $ip ); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
38
|
|
|
|
|
138
|
my $reverse = $ip->reverse_ip; |
27
|
38
|
|
|
|
|
1881
|
foreach my $zone ( @roots ) { |
28
|
40
|
|
|
|
|
1121
|
my $domain = $zone->name->string; |
29
|
40
|
|
|
|
|
175
|
my $pair = { |
30
|
|
|
|
|
|
|
'in-addr.arpa.' => "origin.$domain", |
31
|
|
|
|
|
|
|
'ip6.arpa.' => "origin6.$domain", |
32
|
|
|
|
|
|
|
}; |
33
|
40
|
|
|
|
|
68
|
foreach my $root ( keys %{$pair} ) { |
|
40
|
|
|
|
|
129
|
|
34
|
65
|
100
|
|
|
|
701
|
if ( $reverse =~ s/$root/$pair->{$root}/ix ) { |
35
|
38
|
|
|
|
|
146
|
my $p = $zone->query_persistent( $reverse, 'TXT' ); |
36
|
38
|
100
|
|
|
|
143
|
next if not $p; |
37
|
|
|
|
|
|
|
|
38
|
37
|
|
|
|
|
122
|
my ( $rr ) = $p->get_records( 'TXT' ); |
39
|
37
|
50
|
|
|
|
240
|
return if not $rr; |
40
|
|
|
|
|
|
|
|
41
|
37
|
|
|
|
|
1472
|
my $str = $rr->txtdata; |
42
|
37
|
|
|
|
|
482
|
$str =~ s/"([^"]+)"/$1/x; |
43
|
37
|
|
|
|
|
232
|
my @fields = split( /[ ]\|[ ]?/x, $str ); |
44
|
37
|
|
|
|
|
127
|
my @asns = split( /\s+/x, $fields[0] ); |
45
|
|
|
|
|
|
|
|
46
|
37
|
|
|
|
|
183
|
return \@asns, Zonemaster::Engine::Net::IP->new( $fields[1] ), $str; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} ## end foreach my $zone ( @roots ) |
50
|
1
|
|
|
|
|
9
|
return; |
51
|
|
|
|
|
|
|
} ## end sub get_with_prefix |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub get { |
54
|
1
|
|
|
1
|
1
|
1009
|
my ( $class, $ip ) = @_; |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
5
|
my ( $asnref, $prefix, $raw ) = $class->get_with_prefix( $ip ); |
57
|
|
|
|
|
|
|
|
58
|
1
|
50
|
|
|
|
5
|
if ( $asnref ) { |
59
|
1
|
|
|
|
|
3
|
return @{$asnref}; |
|
1
|
|
|
|
|
6
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else { |
62
|
0
|
|
|
|
|
|
return; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 NAME |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Zonemaster::Engine::ASNLookup - do lookups of ASNs for IP addresses |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my ($asnref, $prefix) = Zonemaster::Engine::ASNLookup->get_with_prefix( '8.8.4.4' ); |
75
|
|
|
|
|
|
|
my $asnref = Zonemaster::Engine::ASNLookup->get( '192.168.0.1' ); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 FUNCTION |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=over |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item get($addr) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Takes a string (or a L<Net::IP> object) with a single IP address, does a lookup |
84
|
|
|
|
|
|
|
in a Cymru-style DNS zone and returns a list of AS numbers for the address, if |
85
|
|
|
|
|
|
|
any can be found. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item get_with_prefix($addr) |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
As L<get()>, except it returns a list of a reference to a list with the AS |
90
|
|
|
|
|
|
|
numbers, and a Net::IP object representing the prefix of the AS. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=back |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |