diff --git a/Changes b/Changes index 316a789..72e174a 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for Perl extension NetAddr::IP +4.059 Wed Mar 7 12:50:04 PST 2012 + add is_rfc1918 to Lite.pm v1.42 + + documentation updates + 4.058 Fri Nov 11 19:23:11 PST 2011 up rev for documentation update update documentation in InetBase v0.06 @@ -201,7 +206,7 @@ for spotting and replicating that bug added new_cis and new_cis6 to Lite.pm v1.21 to accomodate - Cisco syntax that has IP and MASK seperated by a space. + Cisco syntax that has IP and MASK separated by a space. Updated typo in documentation that left out example print new6 stuff diff --git a/IP.pm b/IP.pm index f5eccd8..e57d786 100644 --- a/IP.pm +++ b/IP.pm @@ -4,7 +4,7 @@ use strict; #use diagnostics; -use NetAddr::IP::Lite 1.41 qw(Zero Zeros Ones V4mask V4net); +use NetAddr::IP::Lite 1.42 qw(Zero Zeros Ones V4mask V4net); use NetAddr::IP::Util 1.46 qw( sub128 inet_aton @@ -35,7 +35,7 @@ @ISA = qw(Exporter NetAddr::IP::Lite); -$VERSION = do { sprintf " %d.%03d", (q$Revision: 4.58 $ =~ /\d+/g) }; +$VERSION = do { sprintf " %d.%03d", (q$Revision: 4.59 $ =~ /\d+/g) }; =pod @@ -70,13 +70,13 @@ See L - my $ip = new NetAddr::IP::Lite '127.0.0.1'; + my $ip = new NetAddr::IP '127.0.0.1'; or if your prefer - my $ip = NetAddr::IP::Lite->new('127.0.0.1); + my $ip = NetAddr::IP->new('127.0.0.1); or from a packed IPv4 address - my $ip = new_from_aton NetAddr::IP::Lite (inet_aton('127.0.0.1')); + my $ip = new_from_aton NetAddr::IP (inet_aton('127.0.0.1')); or from an octal filtered IPv4 address - my $ip = new_no NetAddr::IP::Lite '127.012.0.0'; + my $ip = new_no NetAddr::IP '127.012.0.0'; print "The address is ", $ip->addr, " with mask ", $ip->mask, "\n" ; @@ -188,7 +188,7 @@ This module provides an object-oriented abstraction on top of IP addresses or IP subnets, that allows for easy manipulations. -Version 4.xx of NetAdder::IP will will work with older +Version 4.xx of NetAdder::IP will work with older versions of Perl and is compatible with Math::BigInt. The internal representation of all IP objects is in 128 bit IPv6 notation. @@ -275,12 +275,12 @@ This operation changes the address part to point so many hosts above the current objects start address. For instance, this code: - print NetAddr::IP::Lite->new('127.0.0.1/8') + 5; + print NetAddr::IP->new('127.0.0.1/8') + 5; will output 127.0.0.6/8. The address will wrap around at the broadcast back to the network address. This code: - print NetAddr::IP::Lite->new('10.0.0.1/24') + 255; + print NetAddr::IP->new('10.0.0.1/24') + 255; outputs 10.0.0.0/24. @@ -295,7 +295,7 @@ =item B)> -Returns the difference between the address parts of two NetAddr::IP::Lite +Returns the difference between the address parts of two NetAddr::IP objects address parts as a 32 bit signed number. Returns B if the difference is out of range. @@ -712,7 +712,7 @@ When called in scalar context, will return a Math::BigInt representation of the address part of the IP address. When called in -an array context, it returns a list of tow elements, The first +an array context, it returns a list of two elements, The first element is as described, the second element is the Math::BigInt representation of the netmask. @@ -860,6 +860,14 @@ Note that C<$me> and C<$other> must be C objects. +=item C-Eis_rfc1918()> + +Returns true when C<$me> is an RFC 1918 address. + + 10.0.0.0 - 10.255.255.255 (10/8 prefix) + 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) + 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) + =item C<-Esplitref($bits,[optional $bits1,$bits2,...])> Returns a reference to a list of objects, representing subnets of C mask @@ -1565,7 +1573,8 @@ =head1 SEE ALSO - perl(1),NetAddr::IP::Lite, NetAddr::IP::Util. + perl(1) L, L, +L =cut diff --git a/Lite/Changes b/Lite/Changes index c0d9620..f09a25f 100644 --- a/Lite/Changes +++ b/Lite/Changes @@ -1,5 +1,10 @@ Revision history for Perl extension NetAddr::IP::Lite +1.42 Wed Mar 7 12:50:04 PST 2012 + add "is_rfc1918" and tests + + correct typos in documentation + 1.41 Fri Nov 11 19:23:11 PST 2011 up rev for documentation update update documentation in InetBase v0.06 @@ -164,7 +169,7 @@ for spotting and replicating that bug added new_cis and new_cis6 to Lite.pm v1.21 to accomodate - Cisco syntax that has IP and MASK seperated by a space. + Cisco syntax that has IP and MASK separated by a space. Updated typo in documentation that left out example print new6 stuff diff --git a/Lite/Lite.pm b/Lite/Lite.pm index 38b3bd0..7a5dced 100644 --- a/Lite/Lite.pm +++ b/Lite/Lite.pm @@ -32,7 +32,7 @@ use vars qw(@ISA @EXPORT_OK $VERSION $Accept_Binary_IP $Old_nth $AUTOLOAD *Zero); -$VERSION = do { my @r = (q$Revision: 1.41 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; +$VERSION = do { my @r = (q$Revision: 1.42 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; require Exporter; @@ -1324,6 +1324,37 @@ ? 1 : 0; } +=item C-Eis_rfc1918()> + +Returns true when C<$me> is an RFC 1918 address. + + 10.0.0.0 - 10.255.255.255 (10/8 prefix) + 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) + 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) + +=cut + +my $ip_10 = NetAddr::IP::Lite->new('10.0.0.0/8'); +my $ip_10n = $ip_10->{addr}; # already the right value +my $ip_10b = $ip_10n | ~ $ip_10->{mask}; + +my $ip_172 = NetAddr::IP::Lite->new('172.16.0.0/12'); +my $ip_172n = $ip_172->{addr}; # already the right value +my $ip_172b = $ip_172n | ~ $ip_172->{mask}; + +my $ip_192 = NetAddr::IP::Lite->new('192.168.0.0/16'); +my $ip_192n = $ip_192->{addr}; # already the right value +my $ip_192b = $ip_192n | ~ $ip_192->{mask}; + +sub is_rfc1918 ($) { + my $netme = $_[0]->{addr} & $_[0]->{mask}; + my $brdme = $_[0]->{addr} | ~ $_[0]->{mask}; + return 1 if (sub128($netme,$ip_10n) && sub128($ip_10b,$brdme)); + return 1 if (sub128($netme,$ip_192n) && sub128($ip_192b,$brdme)); + return (sub128($netme,$ip_172n) && sub128($ip_172b,$brdme)) + ? 1 : 0; +} + =item C<-Efirst()> Returns a new object representing the first usable IP address within diff --git a/Lite/MANIFEST b/Lite/MANIFEST index 600fe4c..af981dd 100644 --- a/Lite/MANIFEST +++ b/Lite/MANIFEST @@ -40,6 +40,7 @@ t/pathological.t t/range.t t/relops.t +t/rfc_1918.t t/v4-aton.t t/v4-badnm.t t/v4-base.t diff --git a/Lite/README b/Lite/README index bbd0e57..21845a2 100644 --- a/Lite/README +++ b/Lite/README @@ -367,6 +367,13 @@ completely contained within "$other", undef if "$me" and "$other" are not both "NetAddr::IP::Lite" objects. + C->is_rfc1918()> + Returns true when "$me" is an RFC 1918 address. + + 10.0.0.0 - 10.255.255.255 (10/8 prefix) + 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) + 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) + "->first()" Returns a new object representing the first usable IP address within the subnet (ie, the first host address). diff --git a/Lite/Util/Changes b/Lite/Util/Changes index e57530f..b91d4ae 100644 --- a/Lite/Util/Changes +++ b/Lite/Util/Changes @@ -1,3 +1,7 @@ +1.47 + correct typo in documentation POD + Thanks to Gregor Herrmann, Debian Perl Group + 1.46 Fri Nov 11 19:23:11 PST 2011 update documentation in InetBase v0.06 diff --git a/Lite/Util/README b/Lite/Util/README index 76a846e..8c6f7df 100644 --- a/Lite/Util/README +++ b/Lite/Util/README @@ -382,7 +382,7 @@ Replacement for Perl's gethostbyname if Socket6 is available In ARRAY context, returns a list of five elements, the hostname or - NAME, a space seperated list of C_NAMES, AF family, length of the + NAME, a space separated list of C_NAMES, AF family, length of the address structure, and an array of one or more netaddr's In SCALAR context, returns the first netaddr. diff --git a/Lite/Util/Util.pm b/Lite/Util/Util.pm index e328902..c4e62bd 100644 --- a/Lite/Util/Util.pm +++ b/Lite/Util/Util.pm @@ -21,7 +21,7 @@ @ISA = qw(Exporter DynaLoader); -$VERSION = do { my @r = (q$Revision: 1.46 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; +$VERSION = do { my @r = (q$Revision: 1.47 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; @EXPORT_OK = qw( inet_aton @@ -730,7 +730,7 @@ Replacement for Perl's gethostbyname if Socket6 is available In ARRAY context, returns a list of five elements, the hostname or NAME, -a space seperated list of C_NAMES, AF family, length of the address +a space separated list of C_NAMES, AF family, length of the address structure, and an array of one or more netaddr's In SCALAR context, returns the first netaddr. diff --git a/Lite/t/rfc_1918.t b/Lite/t/rfc_1918.t new file mode 100644 index 0000000..850de21 --- /dev/null +++ b/Lite/t/rfc_1918.t @@ -0,0 +1,38 @@ + +#use diagnostics; +use NetAddr::IP::Lite; + +$| = 1; + +print "1..12\n"; + +my $test = 1; +sub ok() { + print 'ok ',$test++,"\n"; +} + +my @ips = qw( + 9.255.255.255 0 + 10.0.0.0 1 + 10.255.255.255 1 + 11.0.0.0 0 + 172.15.255.255 0 + 172.16.0.0 1 + 172.31.255.255 1 + 172.32.0.0 0 + 192.167.255.255 0 + 192.168.0.0 1 + 192.168.255.255 1 + 192.169.0.0 0 +); + + +for (my $i=0;$i<=$#ips;$i+=2) { + my $ip = new NetAddr::IP::Lite($ips[$i]); + my $got = $ip->is_rfc1918(); + my $exp = $ips[$i+1]; + print $ip," got: $got, exp: $exp\nnot " + unless $got == $exp; + &ok; +} + diff --git a/MANIFEST b/MANIFEST index 6a369ec..ba10a4c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -78,6 +78,7 @@ Lite/t/pathological.t Lite/t/range.t Lite/t/relops.t +Lite/t/rfc_1918.t Lite/t/v4-aton.t Lite/t/v4-badnm.t Lite/t/v4-base.t diff --git a/META.yml b/META.yml index c665329..ec28ad6 100644 --- a/META.yml +++ b/META.yml @@ -1,6 +1,6 @@ --- #YAML:1.0 name: NetAddr-IP -version: 4.058 +version: 4.059 abstract: Manages IPv4 and IPv6 addresses and subnets license: ~ author: