Zonemaster/Engine/ASNLookup.pm untested

filter: Statementstmt 0.0% 0 / 140 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 233 CC 27 SCAR 45.2
CC
27
Cov
0%
_cymru_asn_lookup
210.0
get_with_prefix
56.0
_ripe_asn_lookup
56.0
CRAP 91.7
1 package Zonemaster::Engine::ASNLookup;
2  
3 use v5.16.0;
4 use warnings;
5  
6 use version; our $VERSION = version->declare( "v1.0.11" );
7  
8 use Zonemaster::Engine;
9 use Zonemaster::Engine::Util qw( name );
10 use Zonemaster::Engine::Nameserver;
11 use Zonemaster::Engine::Profile;
12  
13 use IO::Socket;
14 use IO::Socket::INET;
15 use Net::IP::XS;
16 use Scalar::Util qw( looks_like_number );
17  
18 our @db_sources;
19 our $db_style;
20  
21 sub get_with_prefix {
22 my ( $class, $ip ) = @_;
23  
24 if ( not @db_sources ) {
25 $db_style = Zonemaster::Engine::Profile->effective->get( q{asn_db.style} );
26 my %db_sources = %{ Zonemaster::Engine::Profile->effective->get( q{asn_db.sources} ) };
27 @db_sources = map { name( $_ ) } @{ $db_sources{ $db_style } };
28 }
29  
30 if ( not ref( $ip ) or not $ip->isa( 'Net::IP::XS' ) ) {
31 $ip = Net::IP::XS->new( $ip );
32 }
33  
34 if ( not @db_sources ) {
35 die "ASN database sources undefined";
36 }
37  
38 my ( $asnref, $prefix, $raw, $ret_code );
39  
40 if ( $db_style eq q{cymru} ) {
41 ( $asnref, $prefix, $raw, $ret_code ) = _cymru_asn_lookup($ip);
42 }
43 elsif ( $db_style eq q{ripe} ) {
44 ( $asnref, $prefix, $raw, $ret_code ) = _ripe_asn_lookup($ip);
45 }
46 else {
47 if ( not $db_style ) {
48 die "ASN database style undefined";
49 }
50 else {
51 die "ASN database style value '$db_style' is illegal";
52 }
53 }
54  
55 map { looks_like_number( $_ ) || die "ASN lookup value isn't numeric: '$_'" } @$asnref;
56  
57 return ( $asnref, $prefix, $raw, $ret_code );
58  
59 } ## end sub get_with_prefix
60  
61 sub _cymru_asn_lookup {
62 my $ip = shift;
63 my @asns = ();
64  
65 my $db_source_nb = 0;
66 foreach my $db_source ( @db_sources ) {
67 Zonemaster::Engine->logger->add( ASN_LOOKUP_SOURCE => { name => $db_source } );
68 my $reverse = $ip->reverse_ip;
69 my $domain = $db_source->string;
70 my $pair = {
71 'in-addr.arpa.' => "origin.$domain",
72 'ip6.arpa.' => "origin6.$domain",
73 };
74 $db_source_nb++;
75  
76 foreach my $root ( keys %{$pair} ) {
77 if ( $reverse =~ s/$root/$pair->{$root}/ix ) {
78 my $p = Zonemaster::Engine->recurse( $reverse, 'TXT' );
79  
80 if ( $p ) {
81 if ( $p->rcode eq q{NXDOMAIN} ) {
82 if ( $p->get_records( 'SOA', 'authority' ) and scalar $p->get_records( 'SOA', 'authority' ) == 1 and ($p->get_records( 'SOA', 'authority' ))[0]->owner eq name( $db_source ) ) {
83 return \@asns, undef, q{}, q{EMPTY_ASN_SET};
84 }
85 }
86 elsif ( $p->rcode eq q{NOERROR} ) {
87 if ( $p->answer ) {
88 my @rr = $p->get_records( 'TXT', 'answer' );
89  
90 if ( @rr ) {
91 my $max_length = 0;
92 my @fields;
93 my $str;
94  
95 foreach my $rr ( @rr ) {
96 my $_str = $rr->txtdata;
97 my @_fields = split( /[ ][|][ ]?/x, $_str );
98  
99 next if scalar @_fields <= 1;
100 return \@asns, undef, q{}, q{ERROR_ASN_DATABASE} unless Net::IP::XS->new( $_fields[1] )->overlaps( $ip );
101  
102 my @_asns = split( /\s+/x, $_fields[0] );
103 my $_prefix_length = ($_fields[1] =~ m!^.*[/](.*)!x)[0];
104 if ( $_prefix_length > $max_length ) {
105 $str = $_str;
106 @asns = @_asns;
107 @fields = @_fields;
108 $max_length = $_prefix_length;
109 }
110 }
111  
112 if ( @fields ) {
113 if ( Net::IP::XS->new( $fields[1] )->overlaps( $ip ) ) {
114 return \@asns, Net::IP::XS->new( $fields[1] ), $str, q{AS_FOUND}
115 }
116 }
117 else {
118 return \@asns, undef, q{}, q{EMPTY_ASN_SET};
119 }
120 }
121  
122 return \@asns, undef, q{}, q{ERROR_ASN_DATABASE};
123 }
124  
125 return \@asns, undef, q{}, q{EMPTY_ASN_SET};
126 }
127 }
128  
129 if ( $db_source_nb == scalar @db_sources ) {
130 return \@asns, undef, q{}, q{ERROR_ASN_DATABASE};
131 }
132  
133 last;
134 }
135 }
136 } ## end foreach my $db_source ( @db_sources )
137 return;
138 }
139  
140 sub _ripe_asn_lookup {
141 my $ip = shift;
142 my @asns = ();
143  
144 my $db_source_nb = 0;
145 foreach my $db_source ( @db_sources ) {
146 $db_source_nb++;
147 my $socket = IO::Socket::INET->new( PeerAddr => $db_source->string,
148 PeerPort => q{43},
149 Proto => q{tcp} );
150 unless ( $socket ) {
151 if ( $db_source_nb == scalar @db_sources ) {
152 return \@asns, undef, q{}, q{ERROR_ASN_DATABASE};
153 }
154 else {
155 next;
156 }
157 };
158  
159 printf $socket "-F -M %s\n", $ip->short();
160  
161 my $data;
162 my $str;
163 my $has_answer = 0;
164 while ( defined ($data = <$socket>) ) {
165 $has_answer = 1;
166 chop $data;
167 if ( $data !~ /^%/x and $data !~ /^\s*$/x ) {
168 $str = $data;
169 last;
170 }
171 }
172 $socket->close();
173 if ( not $has_answer ) {
174 if ( $db_source_nb == scalar @db_sources ) {
175 return \@asns, undef, q{}, q{ERROR_ASN_DATABASE};
176 }
177 else {
178 next;
179 }
180 }
181 elsif ( $str ) {
182 my @fields = split( /\s+/x, $str );
183 my @asns = split( '/', $fields[0] );
184 return \@asns, Net::IP::XS->new( $fields[1] ), $str, q{AS_FOUND};
185 }
186 else {
187 return \@asns, undef, q{}, q{EMPTY_ASN_SET};
188 }
189 } ## end foreach my $db_source ( @
190 return;
191 }
192  
193 sub get {
194 my ( $class, $ip ) = @_;
195  
196 my ( $asnref, $prefix, $raw, $ret_code ) = $class->get_with_prefix( $ip );
197  
198 if ( $asnref ) {
199 return @{$asnref};
200 }
201 else {
202 return;
203 }
204 }
205  
206 1;
207  
208 =head1 NAME
209  
210 Zonemaster::Engine::ASNLookup - do lookups of ASNs for IP addresses
211  
212 =head1 SYNOPSIS
213  
214 my ( $asnref, $prefix, $raw, $ret_code ) = Zonemaster::Engine::ASNLookup->get_with_prefix( '8.8.4.4' );
215 my $asnref = Zonemaster::Engine::ASNLookup->get( '192.168.0.1' );
216  
217 =head1 FUNCTION
218  
219 =over
220  
221 =item get($addr)
222  
223 As L<get_with_prefix()>, except it returns only the list of AS numbers
224 for the address, if any.
225  
226 =item get_with_prefix($addr)
227  
228 Takes a string (or a L<Net::IP::XS> object) with a single IP address, and
229 does a lookup in either: a) Cymru-style DNS zone or b) RIPE whois server,
230 depending on L<Zonemaster::Engine::Profile> setting "asn_db{style}".
231  
232 Returns a list of a reference to a list of AS numbers, a Net::IP::XS object
233 of the covering prefix for that AS, a string of the raw query, and a string
234 of the return code for that query.
235  
236 =back
237  
238 =cut