Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Checking IP's with an IP/mask

by repson (Chaplain)
on Apr 24, 2001 at 09:40 UTC ( #74965=note: print w/ replies, xml ) Need Help??


in reply to Re: Checking IP's with an IP/mask
in thread Checking IP's with an IP/mask

I don't know much about network addresses, but isn't something like this sufficient?

use Socket; sub IsInRange { my ($addr,$base) = @_; my $bits; ($base, $bits) = split /\//, $base; return ( (unpack("N", inet_aton($base)) >> (32 - $bits)) == (unpack ("N", inet_aton($addr)) >> (32 - $bits)) ); }


Comment on Re: Re: Checking IP's with an IP/mask
Download Code
Re^3: Checking IP's with an IP/mask
by tadman (Prior) on Apr 24, 2001 at 18:49 UTC
    This should work too, and is probably better because of simplicity.

    For those wondering, what this code effectively does is compare the network numbers of the $addr and the $base specification by shifting the host bits off the end (with the >> operator). What I was doing was building a range specification, which given the task, is really overkill.

    Well put.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://74965]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2015-07-03 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls