Skip Menu |

This queue is for tickets about the NetAddr-IP CPAN distribution.

Report information
The Basics
Id: 106437
Status: rejected
Priority: 0/
Queue: NetAddr-IP

People
Owner: Nobody in particular
Requestors: xan [...] biblionix.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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; }
This functionality is already included the "within" method $ip->within(new NetAddr::IP::Lite("127/8"); example: use NetAddr::IP::Lite; use strict; my $ip = new NetAddr::IP::Lite($ARGV[0]); my $localnet = new NetAddr::IP::Lite("127/8"); if ($ip->within($localnet)) { print "yes\n"; } else { print "no\n"; } On Thu Aug 13 18:07:41 2015, xan@biblionix.com wrote: Show quoted text
> 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; > }
Subject: Re: [rt.cpan.org #106437] Request for is_localhost function
Date: Thu, 13 Aug 2015 20:51:05 -0500
To: bug-NetAddr-IP [...] rt.cpan.org
From: Xan Charbonnet <xan [...] biblionix.com>
Thanks for writing back, and thanks for NetAddr::IP! Your example requires a lot of code every time I want to check for a local address. Also, it doesn't account for ::1 as does the code I submitted. It seems to me that my is_localhost is very similar to is_rfc1918 in terms of what it accomplishes and how much effort it saves. I think it fits, but of course the final call is yours. On 08/13/2015 07:05 PM, Michael Robinton via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=106437 > > > This functionality is already included the "within" method > > $ip->within(new NetAddr::IP::Lite("127/8"); > > example: > > use NetAddr::IP::Lite; > > use strict; > > my $ip = new NetAddr::IP::Lite($ARGV[0]); > > my $localnet = new NetAddr::IP::Lite("127/8"); > > if ($ip->within($localnet)) { > print "yes\n"; > } else { > print "no\n"; > } > > > > On Thu Aug 13 18:07:41 2015, xan@biblionix.com wrote:
>> 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; >> }
> > >