Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: IP in range

by smahesh (Pilgrim)
on Jan 07, 2011 at 03:27 UTC ( #880996=note: print w/replies, xml ) Need Help??

in reply to IP in range


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 = = 0x01020304 = 1057540 (decimal)
End IP = = 0x0C06FFFF = 201785343 (decimal)
Target IP = = 0x80010003 = 2147549187 (decimal)
2147549187 is not between 1057540 and 201785343 - so target ip is not in range.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://880996]
[Corion]: ambrus: "the other" ;) To me, "normal" for GUIs means (0,0) is left-upper corner of the monitor/window. But for Prima, the lower-left corner it is ;)
[Corion]: Which would be OK for mathematics and paper, for me ;)
[ambrus]: Ok, so Prima has a postscript-like coordinate system
[Corion]: ambrus: Yes, which kinda makes sense because Prima also has Postscript output or something, I think

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2016-12-09 10:49 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.