Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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 cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2017-06-28 02:05 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (619 votes). Check out past polls.