Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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)) ); }

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://74965]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2018-01-18 14:43 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (212 votes). Check out past polls.