Subject: | Request for is_localhost function |
Date: | Thu, 13 Aug 2015 17:07:21 -0500 |
To: | bug-NetAddr-IP [...] rt.cpan.org |
From: | Xan Charbonnet <xan [...] biblionix.com> |
Hi,
In addition to the is_rfc1918 function, I've found it would be helpful
to have a is_localhost function. I propose the following:
=item C-E<gt>is_localhost()>
Returns true when C<$me> is a localhost address.
127.0.0.0 - 127.255.255.255 (127/8 prefix)
::1
=cut
my $ip_127 = NetAddr::IP::Lite->new('127.0.0.0/8');
my $ip_127n = $ip_127->{addr};
my $ip_127b = $ip_127n | ~ $ip_127->{mask};
sub is_localhost ($) {
return 1 if !hasbits(~$_[0]->{mask}) &&
!vec($_[0]->{addr},0,32) &&
!vec($_[0]->{addr},1,32) &&
!vec($_[0]->{addr},2,32) &&
vec($_[0]->{addr},3,32) == 1;
my $netme = $_[0]->{addr} & $_[0]->{mask};
my $brdme = $_[0]->{addr} | ~ $_[0]->{mask};
return 1 if (sub128($netme,$ip_127n) && sub128($ip_127b,$brdme));
return 0;
}