Skip to content
Merged

Tidy #1561

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions .perltidyrc
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
-ole=unix
-se
-bext=~
-l=120
# Locked defaults
-i=4
-ci=2
-nsbl
-cti=0
-pt=0
-sbt=1
-bt=1
-bbt=0
-sfs
-tso
-csc
-csci=20
-nbbc

# Local policy
-conv # Iterate application of perltidy profile to come closer to idempotency
-se # Print errors to stderr instead of a file
-ole=unix # Standardize on LF line endings
-bext=~ # Use ~ as backup extension instead of .bak
-l=120 # Modern line length
-pt=0 # Put whitespace inside parentheses
-tso # Format things like 0+ and +0 without space in between
-csc -csci=20 # Add closing comments to blocks of at least 20 lines
-nbbc # Prevent automatic blank lines before comments
-wtc=m -atc -dtc # Add trailing commas to multi-line lists, and remove them from single-line lists
7 changes: 4 additions & 3 deletions lib/Zonemaster/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package Zonemaster::Engine;
use v5.16.0;
use warnings;

use version; our $VERSION = version->declare("v9.0.1");
use version; our $VERSION = version->declare( "v9.0.1" );

BEGIN {
# Locale::TextDomain (<= 1.20) doesn't know about File::ShareDir so give a helping hand.
Expand All @@ -30,6 +30,7 @@ our $logger;
our $recursor = Zonemaster::Engine::Recursor->new;

my $init_done;

sub init_engine {
return if $init_done++;
Zonemaster::Engine::Recursor::init_recursor();
Expand Down Expand Up @@ -128,7 +129,7 @@ sub add_fake_delegation {
or croak 'Each key of argument $href must omit the trailing dot, or it must be a single dot';
( !defined $href->{$name} or ref $href->{$name} eq 'ARRAY' )
or croak 'Each value of argument $href must be an arrayref or undef';
$href->{$name} //= []; # normalize undef to empty arrayref
$href->{$name} //= []; # normalize undef to empty arrayref
}

# Check fake delegation
Expand Down Expand Up @@ -171,7 +172,7 @@ sub add_fake_delegation {
return;
}
return 1;
}
} ## end sub add_fake_delegation

sub add_fake_ds {
my ( $class, $domain, $aref ) = @_;
Expand Down
74 changes: 40 additions & 34 deletions lib/Zonemaster/Engine/ASNLookup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sub get_with_prefix {
if ( not @db_sources ) {
$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 { name( $_ ) } @{ $db_sources{ $db_style } };
@db_sources = map { name( $_ ) } @{ $db_sources{$db_style} };
}

if ( not ref( $ip ) or not $ip->isa( 'Net::IP::XS' ) ) {
Expand All @@ -38,10 +38,10 @@ sub get_with_prefix {
my ( $asnref, $prefix, $raw, $ret_code );

if ( $db_style eq q{cymru} ) {
( $asnref, $prefix, $raw, $ret_code ) = _cymru_asn_lookup($ip);
( $asnref, $prefix, $raw, $ret_code ) = _cymru_asn_lookup( $ip );
}
elsif ( $db_style eq q{ripe} ) {
( $asnref, $prefix, $raw, $ret_code ) = _ripe_asn_lookup($ip);
( $asnref, $prefix, $raw, $ret_code ) = _ripe_asn_lookup( $ip );
}
else {
if ( not $db_style ) {
Expand All @@ -59,15 +59,15 @@ sub get_with_prefix {
} ## end sub get_with_prefix

sub _cymru_asn_lookup {
my $ip = shift;
my $ip = shift;
my @asns = ();

my $db_source_nb = 0;
foreach my $db_source ( @db_sources ) {
Zonemaster::Engine->logger->add( ASN_LOOKUP_SOURCE => { name => $db_source } );
my $reverse = $ip->reverse_ip;
my $domain = $db_source->string;
my $pair = {
my $domain = $db_source->string;
my $pair = {
'in-addr.arpa.' => "origin.$domain",
'ip6.arpa.' => "origin6.$domain",
};
Expand All @@ -79,7 +79,10 @@ sub _cymru_asn_lookup {

if ( $p ) {
if ( $p->rcode eq q{NXDOMAIN} ) {
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 ) ) {
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 ) )
{
return \@asns, undef, q{}, q{EMPTY_ASN_SET};
}
}
Expand All @@ -93,75 +96,78 @@ sub _cymru_asn_lookup {
my $str;

foreach my $rr ( @rr ) {
my $_str = $rr->txtdata;
my $_str = $rr->txtdata;
my @_fields = split( /[ ][|][ ]?/x, $_str );

next if scalar @_fields <= 1;
return \@asns, undef, q{}, q{ERROR_ASN_DATABASE} unless Net::IP::XS->new( $_fields[1] )->overlaps( $ip );
return \@asns, undef, q{}, q{ERROR_ASN_DATABASE}
unless Net::IP::XS->new( $_fields[1] )->overlaps( $ip );

my @_asns = split( /\s+/x, $_fields[0] );
my $_prefix_length = ($_fields[1] =~ m!^.*[/](.*)!x)[0];
my @_asns = split( /\s+/x, $_fields[0] );
my $_prefix_length = ( $_fields[1] =~ m!^.*[/](.*)!x )[0];
if ( $_prefix_length > $max_length ) {
$str = $_str;
@asns = @_asns;
@fields = @_fields;
$str = $_str;
@asns = @_asns;
@fields = @_fields;
$max_length = $_prefix_length;
}
}

if ( @fields ) {
if ( Net::IP::XS->new( $fields[1] )->overlaps( $ip ) ) {
return \@asns, Net::IP::XS->new( $fields[1] ), $str, q{AS_FOUND}
return \@asns, Net::IP::XS->new( $fields[1] ), $str, q{AS_FOUND};
}
}
else {
return \@asns, undef, q{}, q{EMPTY_ASN_SET};
}
}
} ## end if ( @rr )

return \@asns, undef, q{}, q{ERROR_ASN_DATABASE};
}
} ## end if ( $p->answer )

return \@asns, undef, q{}, q{EMPTY_ASN_SET};
}
}
} ## end elsif ( $p->rcode eq q{NOERROR})
} ## end if ( $p )

if ( $db_source_nb == scalar @db_sources ) {
return \@asns, undef, q{}, q{ERROR_ASN_DATABASE};
}

last;
}
}
} ## end foreach my $db_source ( @db_sources )
} ## end if ( $reverse =~ s/$root/$pair->{$root}/ix)
} ## end foreach my $root ( keys %{$pair...})
} ## end foreach my $db_source ( @db_sources)
return;
}
} ## end sub _cymru_asn_lookup

sub _ripe_asn_lookup {
my $ip = shift;
my $ip = shift;
my @asns = ();

my $db_source_nb = 0;
foreach my $db_source ( @db_sources ) {
$db_source_nb++;
my $socket = IO::Socket::INET->new( PeerAddr => $db_source->string,
PeerPort => q{43},
Proto => q{tcp} );
unless ( $socket ) {
my $socket = IO::Socket::INET->new(
PeerAddr => $db_source->string,
PeerPort => q{43},
Proto => q{tcp},
);
unless ( $socket ) {
if ( $db_source_nb == scalar @db_sources ) {
return \@asns, undef, q{}, q{ERROR_ASN_DATABASE};
}
else {
next;
}
};
}

printf $socket "-F -M %s\n", $ip->short();

my $data;
my $str;
my $has_answer = 0;
while ( defined ($data = <$socket>) ) {
while ( defined( $data = <$socket> ) ) {
$has_answer = 1;
chop $data;
if ( $data !~ /^%/x and $data !~ /^\s*$/x ) {
Expand All @@ -178,17 +184,17 @@ sub _ripe_asn_lookup {
next;
}
}
elsif ( $str ) {
elsif ( $str ) {
my @fields = split( /\s+/x, $str );
my @asns = split( '/', $fields[0] );
my @asns = split( '/', $fields[0] );
return \@asns, Net::IP::XS->new( $fields[1] ), $str, q{AS_FOUND};
}
else {
return \@asns, undef, q{}, q{EMPTY_ASN_SET};
}
} ## end foreach my $db_source ( @
} ## end foreach my $db_source ( @db_sources)
return;
}
} ## end sub _ripe_asn_lookup

sub get {
my ( $class, $ip ) = @_;
Expand Down
75 changes: 42 additions & 33 deletions lib/Zonemaster/Engine/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package Zonemaster::Engine::Constants;
use v5.16.0;
use warnings;

use version; our $VERSION = version->declare("v1.2.5");
use version; our $VERSION = version->declare( "v1.2.5" );

use Carp;
use English qw( -no_match_vars ) ;
use English qw( -no_match_vars );
use parent 'Exporter';
use Net::IP::XS;
use Text::CSV;
Expand Down Expand Up @@ -100,14 +100,15 @@ our %EXPORT_TAGS = (
algo => [
qw($ALGO_STATUS_DEPRECATED $ALGO_STATUS_PRIVATE $ALGO_STATUS_RESERVED $ALGO_STATUS_UNASSIGNED $ALGO_STATUS_OTHER $ALGO_STATUS_NOT_ZONE_SIGN $ALGO_STATUS_NOT_RECOMMENDED)
],
cname => [ qw($CNAME_MAX_CHAIN_LENGTH $CNAME_MAX_RECORDS) ],
name => [qw($FQDN_MAX_LENGTH $LABEL_MAX_LENGTH)],
ip => [qw($IP_VERSION_4 $IP_VERSION_6)],
soa => [
cname => [qw($CNAME_MAX_CHAIN_LENGTH $CNAME_MAX_RECORDS)],
name => [qw($FQDN_MAX_LENGTH $LABEL_MAX_LENGTH)],
ip => [qw($IP_VERSION_4 $IP_VERSION_6)],
soa => [
qw($DURATION_5_MINUTES_IN_SECONDS $DURATION_1_HOUR_IN_SECONDS $DURATION_4_HOURS_IN_SECONDS $DURATION_12_HOURS_IN_SECONDS $DURATION_1_DAY_IN_SECONDS $DURATION_1_WEEK_IN_SECONDS $DURATION_180_DAYS_IN_SECONDS $SERIAL_BITS $SERIAL_MAX_VARIATION)
],
misc => [qw($UDP_PAYLOAD_LIMIT $EDNS_UDP_PAYLOAD_DNSSEC_DEFAULT $EDNS_UDP_PAYLOAD_DEFAULT $EDNS_UDP_PAYLOAD_COMMON_LIMIT $MINIMUM_NUMBER_OF_NAMESERVERS $BLACKLISTING_ENABLED)]
, # everything in %EXPORT_OK that isn't included in any of the other tags
misc => [
qw($UDP_PAYLOAD_LIMIT $EDNS_UDP_PAYLOAD_DNSSEC_DEFAULT $EDNS_UDP_PAYLOAD_DEFAULT $EDNS_UDP_PAYLOAD_COMMON_LIMIT $MINIMUM_NUMBER_OF_NAMESERVERS $BLACKLISTING_ENABLED)
], # everything in %EXPORT_OK that isn't included in any of the other tags
addresses => [qw(@IPV4_SPECIAL_ADDRESSES @IPV6_SPECIAL_ADDRESSES)],
);

Expand Down Expand Up @@ -199,18 +200,18 @@ Readonly our $ALGO_STATUS_OTHER => 5;
Readonly our $ALGO_STATUS_NOT_ZONE_SIGN => 8;
Readonly our $ALGO_STATUS_NOT_RECOMMENDED => 9;

Readonly our $BLACKLISTING_ENABLED => 1;
Readonly our $BLACKLISTING_ENABLED => 1;

Readonly our $CNAME_MAX_CHAIN_LENGTH => 10;
Readonly our $CNAME_MAX_RECORDS => 9;
Readonly our $CNAME_MAX_CHAIN_LENGTH => 10;
Readonly our $CNAME_MAX_RECORDS => 9;

Readonly our $DURATION_5_MINUTES_IN_SECONDS => 5 * 60;
Readonly our $DURATION_1_HOUR_IN_SECONDS => 60 * 60;
Readonly our $DURATION_4_HOURS_IN_SECONDS => 4 * 60 * 60;
Readonly our $DURATION_12_HOURS_IN_SECONDS => 12 * 60 * 60;
Readonly our $DURATION_1_DAY_IN_SECONDS => 24 * 60 * 60;
Readonly our $DURATION_1_WEEK_IN_SECONDS => 7 * 24 * 60 * 60;
Readonly our $DURATION_180_DAYS_IN_SECONDS => 180 * 24 * 60 * 60;
Readonly our $DURATION_5_MINUTES_IN_SECONDS => 5 * 60;
Readonly our $DURATION_1_HOUR_IN_SECONDS => 60 * 60;
Readonly our $DURATION_4_HOURS_IN_SECONDS => 4 * 60 * 60;
Readonly our $DURATION_12_HOURS_IN_SECONDS => 12 * 60 * 60;
Readonly our $DURATION_1_DAY_IN_SECONDS => 24 * 60 * 60;
Readonly our $DURATION_1_WEEK_IN_SECONDS => 7 * 24 * 60 * 60;
Readonly our $DURATION_180_DAYS_IN_SECONDS => 180 * 24 * 60 * 60;

# Maximum length of ASCII version of a domain name, with trailing dot.
Readonly our $FQDN_MAX_LENGTH => 254;
Expand All @@ -221,16 +222,16 @@ Readonly our $IP_VERSION_6 => 6;

Readonly our $MINIMUM_NUMBER_OF_NAMESERVERS => 2;

Readonly our $SERIAL_BITS => 32;
Readonly our $SERIAL_BITS => 32;
Readonly our $SERIAL_MAX_VARIATION => 0;

Readonly our $UDP_PAYLOAD_LIMIT => 512;
Readonly our $EDNS_UDP_PAYLOAD_DEFAULT => 512;
Readonly our $EDNS_UDP_PAYLOAD_COMMON_LIMIT => 4096;
Readonly our $EDNS_UDP_PAYLOAD_DNSSEC_DEFAULT => 1232;

Readonly::Array our @IPV4_SPECIAL_ADDRESSES => _extract_iana_ip_blocks($IP_VERSION_4);
Readonly::Array our @IPV6_SPECIAL_ADDRESSES => _extract_iana_ip_blocks($IP_VERSION_6);
Readonly::Array our @IPV4_SPECIAL_ADDRESSES => _extract_iana_ip_blocks( $IP_VERSION_4 );
Readonly::Array our @IPV6_SPECIAL_ADDRESSES => _extract_iana_ip_blocks( $IP_VERSION_6 );

=head1 METHODS

Expand All @@ -253,13 +254,15 @@ and C<globally_reachable> (string).

sub _extract_iana_ip_blocks {
my $ip_version = shift;
my @list = ();
my @list = ();

my $csv = Text::CSV->new ({
binary => 1,
auto_diag => 1,
sep_char => q{,}
});
my $csv = Text::CSV->new(
{
binary => 1,
auto_diag => 1,
sep_char => q{,},
}
);
my @files_details = (
{ name => q{iana-ipv4-special-registry.csv}, ip_version => $IP_VERSION_4 },
{ name => q{iana-ipv6-special-registry.csv}, ip_version => $IP_VERSION_6 },
Expand All @@ -268,10 +271,10 @@ sub _extract_iana_ip_blocks {
foreach my $file_details ( @files_details ) {
my $first_line = 1;
next if ${$file_details}{ip_version} != $ip_version;
my $makefile_name = 'Zonemaster-Engine'; # This must be the same name as "name" in Makefile.PL
my $data_location = dist_file($makefile_name, ${$file_details}{name});
open(my $data, '<:encoding(utf8)', $data_location) or croak "Cannot open '${data_location}' : ${OS_ERROR}";
while (my $fields = $csv->getline( $data )) {
my $makefile_name = 'Zonemaster-Engine'; # This must be the same name as "name" in Makefile.PL
my $data_location = dist_file( $makefile_name, ${$file_details}{name} );
open( my $data, '<:encoding(utf8)', $data_location ) or croak "Cannot open '${data_location}' : ${OS_ERROR}";
while ( my $fields = $csv->getline( $data ) ) {
if ( $first_line ) {
$first_line = 0;
next;
Expand All @@ -280,11 +283,17 @@ sub _extract_iana_ip_blocks {
$address_data =~ s/[ ]+//smx;
foreach my $address_item ( split /,/smx, $address_data ) {
$address_item =~ s/(\A.+\/\d+).*\z/$1/smx;
push @list, { ip => Net::IP::XS->new( $address_item ), name => $fields->[1], reference => $fields->[2], globally_reachable => $fields->[8] };
push @list,
{
ip => Net::IP::XS->new( $address_item ),
name => $fields->[1],
reference => $fields->[2],
globally_reachable => $fields->[8],
};
}
}
close $data or croak "Cannot close '${data_location}' : ${OS_ERROR}";
}
} ## end foreach my $file_details ( ...)

return @list;
} ## end sub _extract_iana_ip_blocks
Expand Down
Loading
Loading