rhymejerky has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy to check if an IP is in range or not? For example, say I have this IP range 1.2.3.4 to 6.7.255.9 inclusive. Ip like 5.255.255.255, 2.1.1.1, 12.6.255.255, 1.2.3.4, 1.6.2.5 are considered to be inside the range. I thought about split the ip with '.', Would doing something like
convert 1.2.3.4 => 001002003004 12.6.255.255 => 012006255255 convert the target ip (say 128.1.0.3) to 1280010003
And then do a numeric comparison, would that work?

Replies are listed 'Best First'.
Re: IP in range
by roboticus (Chancellor) on Jan 06, 2011 at 21:11 UTC

    rhymejerky:

    That should work just fine, as that's similar to how IP addresses are typically implemented. Normally in the socket code, the four octets are each a single byte, which are packed into a 32 bit value. You're proposing to perform a similar mapping into a number.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: IP in range
by locked_user sundialsvc4 (Abbot) on Jan 07, 2011 at 01:47 UTC

    With regard to things like this, I have learned (ahem... the hard way) to prefer “clear” over “clever.”

    And to look through http://search.cpan.org before giving the slightest consideration for doing anything myself.   When I search for “IP address range” on that site, I am pointed to a slew of very-recent modules such as NetAddr::IP::Lite and mod:://Net::IP::RangeCompare.   Basically, when I see that, I think:   “okay, this problem is now basically solved.”

    Sometimes you get burned by choosing to grab something recent out of CPAN and use it ... but those times are very rare, indeed.

      Agree, I was looking for CPAN modules for this and turned out the module was not installed on our WS. Also, was thinking about the trade off of loading another module into the pm (probably can use require over use). At the end, I have to do it on my own
Re: IP in range
by ikegami (Patriarch) on Jan 06, 2011 at 22:25 UTC

    "\x01\x02\x03\x04" would also work (inet_aton($_))

    0x01020304 would also work (unpack('N', inet_aton($_)))

    Socket

Re: IP in range
by smahesh (Pilgrim) on Jan 07, 2011 at 03:27 UTC
    Hi,

    I solved this problem in my previous company in Java. I will present the idea here but leave the implementation to you.

    A IPv4 address is just a collection of four octets and can be represented by a 32 bit integer. Just convert the start and end address of the ip range to a 32 bit integer representation. Convert the Ipv4 address to be checked also into a 32 bit number and if the number falls between the start and end numbers, it is in the range. If the address check is called lots of times for a particular range, a integer comparison will be faster than a string comparison.

    For example,
    Start IP = 1.2.3.4 = 0x01020304 = 1057540 (decimal)
    End IP = 12.6.255.255 = 0x0C06FFFF = 201785343 (decimal)
    Target IP = 128.1.0.3 = 0x80010003 = 2147549187 (decimal)
    2147549187 is not between 1057540 and 201785343 - so target ip is not in range.

    Mahesh

A reply falls below the community's threshold of quality. You may see it by logging in.