Skip to content

Commit

Permalink
Make ASNLookup.pm use standard recursive DNS lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreenx committed Nov 28, 2023
1 parent 24f3451 commit 6a90d2b
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 416 deletions.
11 changes: 6 additions & 5 deletions lib/Zonemaster/Engine/ASNLookup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use warnings;
use version; our $VERSION = version->declare( "v1.0.11" );

use Zonemaster::Engine;
use Zonemaster::Engine::Util qw( name );
use Zonemaster::Engine::Nameserver;
use Zonemaster::Engine::Profile;

Expand All @@ -28,7 +29,7 @@ sub get_with_prefix {
#
my @roots;
if ( Zonemaster::Engine::Profile->effective->get( q{asnroots} ) ) {
@roots = map { Zonemaster::Engine->zone( $_ ) } @{ Zonemaster::Engine::Profile->effective->get( q{asnroots} ) };
@roots = map { name( $_ ) } @{ Zonemaster::Engine::Profile->effective->get( q{asnroots} ) };
}
if ( scalar @roots ) {
@db_sources = @roots;
Expand All @@ -37,7 +38,7 @@ sub get_with_prefix {
else {
$db_style = Zonemaster::Engine::Profile->effective->get( q{asn_db.style} );
my %db_sources = %{ Zonemaster::Engine::Profile->effective->get( q{asn_db.sources} ) };
@db_sources = map { Zonemaster::Engine->zone( $_ ) } @{ $db_sources{ $db_style } };
@db_sources = map { name( $_ ) } @{ $db_sources{ $db_style } };
}
}

Expand Down Expand Up @@ -79,15 +80,15 @@ sub _cymru_asn_lookup {
my $reverse = $ip->reverse_ip;
my $db_source_nb = 0;
foreach my $db_source ( @db_sources ) {
my $domain = $db_source->name->string;
my $domain = $db_source->string;
my $pair = {
'in-addr.arpa.' => "origin.$domain",
'ip6.arpa.' => "origin6.$domain",
};
$db_source_nb++;
foreach my $root ( keys %{$pair} ) {
if ( $reverse =~ s/$root/$pair->{$root}/ix ) {
my $p = $db_source->query_persistent( $reverse, 'TXT' );
my $p = Zonemaster::Engine->recurse( $reverse, 'TXT' );
my @rr;
if ( $p ) {
@rr = $p->get_records( 'TXT' );
Expand Down Expand Up @@ -143,7 +144,7 @@ sub _ripe_asn_lookup {
my $db_source_nb = 0;
foreach my $db_source ( @db_sources ) {
$db_source_nb++;
my $socket = IO::Socket::INET->new( PeerAddr => $db_source->name->string,
my $socket = IO::Socket::INET->new( PeerAddr => $db_source->string,
PeerPort => q{43},
Proto => q{tcp} );
unless ( $socket ) {
Expand Down
125 changes: 62 additions & 63 deletions t/Test-connectivity.data

Large diffs are not rendered by default.

246 changes: 105 additions & 141 deletions t/Test-connectivity03.data

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions t/Test-connectivity03.t
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ Zonemaster::Engine::Profile->effective->merge( $profile_test );

###
my $zone = Zonemaster::Engine->zone( q{001.tf} );

zone_gives( $testcase, $zone, [qw{IPV4_ONE_ASN IPV6_ONE_ASN}] );
zone_gives_not( $testcase, $zone, [qw{EMPTY_ASN_SET ERROR_ASN_DATABASE IPV4_DIFFERENT_ASN IPV4_SAME_ASN IPV6_DIFFERENT_ASN IPV6_SAME_ASN}] );

$zone = Zonemaster::Engine->zone( q{zonemaster.net} );
zone_gives( $testcase, $zone, [qw{IPV4_DIFFERENT_ASN IPV6_DIFFERENT_ASN}] );
zone_gives_not( $testcase, $zone, [qw{EMPTY_ASN_SET ERROR_ASN_DATABASE IPV4_SAME_ASN IPV6_SAME_ASN IPV4_ONE_ASN IPV6_ONE_ASN}] );

$zone = Zonemaster::Engine->zone( q{zut-root.rd.nic.fr} );
zone_gives( $testcase, $zone, [qw{IPV4_ONE_ASN}] );
zone_gives_not( $testcase, $zone, [qw{EMPTY_ASN_SET ERROR_ASN_DATABASE IPV4_DIFFERENT_ASN IPV4_SAME_ASN IPV6_DIFFERENT_ASN IPV6_ONE_ASN IPV6_SAME_ASN}] );

TODO: {
my @missing = qw( EMPTY_ASN_SET ERROR_ASN_DATABASE IPV4_DIFFERENT_ASN IPV4_SAME_ASN IPV6_DIFFERENT_ASN IPV6_SAME_ASN );
my @missing = qw( EMPTY_ASN_SET ERROR_ASN_DATABASE IPV4_SAME_ASN IPV6_SAME_ASN );
local $TODO = "Need to find/create zones with those errors: ";
warn $TODO, "\n\t", join("\n\t", @missing), "\n";
}
Expand Down
223 changes: 85 additions & 138 deletions t/Test-connectivity04.data

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions t/Test-connectivity04.t
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ $profile_test = Zonemaster::Engine::Profile->from_json( $json );
Zonemaster::Engine::Profile->effective->merge( $profile_test );

###
my $zone = Zonemaster::Engine->zone( q{001.tf} );

zone_gives( $testcase, $zone, [qw{CN04_IPV4_DIFFERENT_PREFIX CN04_IPV6_DIFFERENT_PREFIX CN04_IPV6_SAME_PREFIX}] );
zone_gives_not( $testcase, $zone, [qw{CN04_IPV4_SAME_PREFIX CN04_EMPTY_PREFIX_SET CN04_ERROR_PREFIX_DATABASE}] );
my $zone = Zonemaster::Engine->zone( q{zonemaster.net} );
zone_gives( $testcase, $zone, [qw{CN04_IPV4_DIFFERENT_PREFIX CN04_IPV6_DIFFERENT_PREFIX}] );
zone_gives_not( $testcase, $zone, [qw{CN04_IPV4_SAME_PREFIX CN04_EMPTY_PREFIX_SET CN04_ERROR_PREFIX_DATABASE CN04_IPV6_SAME_PREFIX}] );

$zone = Zonemaster::Engine->zone( q{zut-root.rd.nic.fr} );
zone_gives( $testcase, $zone, [qw{CN04_IPV4_SAME_PREFIX}] );
zone_gives_not( $testcase, $zone, [qw{CN04_IPV4_DIFFERENT_PREFIX CN04_IPV6_DIFFERENT_PREFIX CN04_IPV6_SAME_PREFIX CN04_EMPTY_PREFIX_SET CN04_ERROR_PREFIX_DATABASE}] );

TODO: {
my @missing = qw( CN04_EMPTY_PREFIX_SET CN04_ERROR_PREFIX_DATABASE );
my @missing = qw( CN04_EMPTY_PREFIX_SET CN04_ERROR_PREFIX_DATABASE CN04_IPV6_SAME_PREFIX );
local $TODO = "Need to find/create zones with those errors: ";
warn $TODO, "\n\t", join("\n\t", @missing), "\n";
}
Expand Down
115 changes: 56 additions & 59 deletions t/asn.data

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions t/asn.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@ if ( not $ENV{ZONEMASTER_RECORD} ) {
Zonemaster::Engine::Profile->effective->set(q{asnroots}, [ "asnlookup.zonemaster.net", "asnlookup.iis.se", "asn.cymru.com" ]);

my ( $asn1, $prefix1 ) = Zonemaster::Engine::ASNLookup->get_with_prefix( '8.8.8.8' );
is $asn1->[0], 15169, '8.8.8.8 is in 15169';
is $asn1->[0], 15169, '8.8.8.8 is in AS15169';
is $prefix1->prefix, '8.8.8.0/24', '8.8.8.8 is in 8.8.8.0/24';

my ( $asn2, $prefix2 ) = Zonemaster::Engine::ASNLookup->get_with_prefix( '91.226.36.46' );
is $asn2->[0], 197564, '91.226.36.46 is in 197564';
is $asn2->[0], 197564, '91.226.36.46 is in AS197564';
is $prefix2->prefix, '91.226.36.0/23', '91.226.36.46 is in 91.226.36.0/24';

my @asn3 = Zonemaster::Engine::ASNLookup->get( '2001:503:ba3e::2:30' );
is( scalar( @asn3 ), 1, 'Only one result' );
ok $asn3[0] >= 36000, '2001:503:ba3e::2:30 is in ' . $asn3[0];
ok $asn3[0] >= 390000, '2001:503:ba3e::2:30 is in AS' . $asn3[0];

my ( $asn4, $prefix4 ) = Zonemaster::Engine::ASNLookup->get_with_prefix( '192.168.0.1' );
ok( scalar @{$asn4} == 0, 'RFC1918 address is in no AS' );

Zonemaster::Engine::Profile->effective->set(q{asn_db.sources}, { cymru => [ "asnlookup.dufberg.se" ] });

my ( $asn5, $prefix5 ) = Zonemaster::Engine::ASNLookup->get_with_prefix( '3.124.111.178' );
is $asn5->[0], 16509, '3.124.111.178 is in AS16509';
is $prefix5->prefix, '3.124.0.0/14', '3.124.111.178 is in 3.124.0.0/14';

if ( $ENV{ZONEMASTER_RECORD} ) {
Zonemaster::Engine::Nameserver->save( $datafile );
}
Expand Down

0 comments on commit 6a90d2b

Please sign in to comment.