From 4b8e83b93cbaeeced828d953d599ce23b1855d43 Mon Sep 17 00:00:00 2001 From: Paulo A Ferreira Date: Mon, 15 Oct 2018 17:53:11 +0100 Subject: [PATCH] Added isreserved to validate reserved ASN numbers by RFC 7300 and RFC 4893 --- examples/astool | 13 ++++++---- lib/Net/ASN.pm | 63 +++++++++++++++++++++++++++++++++++++++---------- t/asn.t | 13 +++++++++- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/examples/astool b/examples/astool index ca3f13e..6c72df8 100755 --- a/examples/astool +++ b/examples/astool @@ -16,9 +16,12 @@ unless ($inasn) { } my $asn = Net::ASN->new($inasn,$asasdot); -print "type : " . $asn->gettype . "\n"; -print "asplain16: " . $asn->toasplain16 . "\n"; -print "asplain32: " . $asn->toasplain . "\n"; -print "asdot : " . $asn->toasdot . "\n"; -print "asdotplus: " . $asn->toasdotplus . "\n"; +print "type : " . $asn->gettype . "\n"; +print "asplain16 : " . $asn->toasplain16 . "\n"; +print "asplain32 : " . $asn->toasplain . "\n"; +print "asdot : " . $asn->toasdot . "\n"; +print "asdotplus : " . $asn->toasdotplus . "\n"; +print "isprivate : " . $asn->isprivate . "\n"; +print "isreserved : " . $asn->isreserved . "\n"; + print "\n"; diff --git a/lib/Net/ASN.pm b/lib/Net/ASN.pm index 1c5a896..fe9fa26 100644 --- a/lib/Net/ASN.pm +++ b/lib/Net/ASN.pm @@ -22,12 +22,12 @@ BEGIN { @EXPORT = qw(); %EXPORT_TAGS = ( - all => [qw{ + all => [qw{ plaintodot plaintodotplus dottodotplus dottoplain dottoplain16 dotplustodot dotplustoplain dotplustoplain16 - isprivateasn + isprivateasn isreservedasn } ], ); @@ -69,7 +69,7 @@ sub new { $self->_parseasn($asn,$asasdot); #Parse the ASN - return $self; + return $self; } @@ -120,7 +120,7 @@ sub _parseasn ($;$) { my $secondasn = $2; unless ( - ($firstasn >= 0 && $firstasn <= AS16_END) && + ($firstasn >= 0 && $firstasn <= AS16_END) && ($secondasn >= 0 && $secondasn <= AS16_END) && ( ($firstasn > 0) || @@ -155,7 +155,7 @@ sub _parseasn ($;$) { } sub _plaintodot ($) { - + my $self = shift; my $asplain = shift; @@ -202,7 +202,7 @@ sub toasdot () { } } - elsif ($self->{_informat} eq 'asdotplus') { #User wants asdot and has given us asdotplus, so return asplain if + elsif ($self->{_informat} eq 'asdotplus') { #User wants asdot and has given us asdotplus, so return asplain if if ($self->{_asdotsedtet1} == 0) { #If first sedtet is 0, return the second only return ($self->{_asdotsedtet2}); } @@ -343,7 +343,20 @@ sub isprivate { return 1; } return 0; - } +} + +##RFC 7300 Reservation of Last Autonomous System (AS) Numbers +##RFC 4893 reserves 23456 for AS_TRANS +sub isreserved { + my $self = shift; + my $asn = $self->toasplain; + if ( $asn == AS16_END || + $asn == AS32_END || + $asn == AS_TRANS ) { + return 1; + } + return 0; +} ###NON OO Function wrappers ##toasdot @@ -416,6 +429,14 @@ sub isprivateasn ($) { return ($asn->isprivate); } +##isreserved +sub isreservedasn ($) { + my $inasn = shift; + croak __PACKAGE__, ": No ASN specified" unless ($inasn); + my $asn = Net::ASN->new($inasn) || croak __PACKAGE__, ": Could not create new Net::ASN object"; + return ($asn->isreserved); +} + 1; __END__ =pod @@ -454,8 +475,8 @@ Net::ASN - Perl extension for manipulating autonomous system numbers =head1 DESCRIPTION -Net::ASN provides functions for parsing autonomous system numbers -(ASNs) as defined in RFC 1771 and extended by RFC4893, also +Net::ASN provides functions for parsing autonomous system numbers +(ASNs) as defined in RFC 1771 and extended by RFC4893, also converting between formats discussed in RFC5396. Both an OO implementation (method based) and non-OO (function based) @@ -494,7 +515,7 @@ Returns the ASPLAIN representation of the parsed ASN Returns the ASPLAIN representation of the parsed ASN if the ASPLAIN representation is is less than or equal to 65535, else returns AS_TRANS -Use for compabability with 16 bit ASN systems. +Use for compatibility with 16 bit ASN systems. =item toasdot @@ -516,7 +537,15 @@ Returns the ASDOT+ representation of the parsed ASN. Returns 1 if the number falls within the private reserved ranges according to RFC6996, 0 otherwise. Will accept any format that L can convert to -ASPLAIN +ASPLAIN + +=item isreserved + + my $isreserved = $asn->isreservedasn; + +Returns 1 if the number falls within the reserved ASN numbers according to +RFC7300 and RFC4893, 0 otherwise. Will accept any format that L can +convert to ASPLAIN =back @@ -591,7 +620,15 @@ representation is is less than or equal to 65535, else returns AS_TRANS Returns 1 if the number falls within the private reserved ranges according to RFC6996, 0 otherwise. Will accept any format that L can convert to -ASPLAIN +ASPLAIN + +=item isreservedasn + + my $isreservedasn = isreservedasn($asn); + +Returns 1 if the number falls within the reserved ASN numbers according to +RFC7300 and RFC4893, 0 otherwise. Will accept any format that L can +convert to ASPLAIN =back @@ -611,7 +648,7 @@ it under the terms as perl itself. =head1 REPOSITORY -L perl(1) diff --git a/t/asn.t b/t/asn.t index 04e561f..e021e97 100755 --- a/t/asn.t +++ b/t/asn.t @@ -1,6 +1,6 @@ #!/usr/bin/perl use Net::ASN qw(:all); -use Test::More tests =>74; +use Test::More tests =>83; ##OO tests print "Starting OO Tests..\n"; @@ -68,6 +68,13 @@ is( $public_asn16->isprivate , 0 ,'16 bit ASN corre ok( $public_asn32 = Net::ASN->new(4199999999) , 'public 32 bit ASN' ); is( $public_asn32->isprivate , 0 ,'32 bit ASN corrected marked as private' ); +ok( $reserved_asn16 = Net::ASN->new(23456), 'AS_TRANS ASN' ); +is( $reserved_asn16->isreserved , 1 ,'AS_TRANS corrected marked as reserved' ); +ok( $reserved_asn16 = Net::ASN->new(65535), 'last 16 bit ASN' ); +is( $reserved_asn16->isreserved , 1 ,'last 16 bit ASN corrected marked as reserved' ); +ok( $reserved_asn32 = Net::ASN->new(4294967295), 'last 32 bit ASN' ); +is( $reserved_asn32->isreserved , 1 ,'last 32 bit ASN corrected marked as reserved' ); + ##Non-OO Tests print "Starting non-OO tests..\n"; @@ -92,3 +99,7 @@ is( isprivateasn(64512) , 1, 'isprivateasn reports private for 16 bit ASN' ) is( isprivateasn(4200000000), 1, 'isprivateasn reports private for 32 bit ASN' ); is( isprivateasn(64511) , 0, 'isprivateasn reports public for 16 bit ASN' ); is( isprivateasn(4199999999), 0, 'isprivateasn reports public for 32 bit ASN' ); + +is( isreservedasn(23456) , 1, 'isreservedasn reports reserved for 16 bit AS_TRANS' ); +is( isreservedasn(65535) , 1, 'isreservedasn reports reserved for last 16 bit ASN' ); +is( isreservedasn(4294967295), 1, 'isreservedasn reports reserved for last 32 bit ASN' );