Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find if IP is in range

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
0
0
US
I have the the CIDR notation. I have the cidr range.

So I have

10.10.10.1/18 and the range 10.10.0.0 - 10.10.63.255

What is the algorithm to see if an IP is in that range. I am trying to write a function in Coldfusion.

so I know 10.10.0.1 is in range but 12.4.2.134 is not. Can someone help. I just need the algorithm.

 
You just need to figure out a way of comparing the first 18 bits of your IP address with your network address, which, in binary, is;

00001010.00001010.00000000.00000000

If these bits are all the same, then your address is within the defined subnet.

How to do that? Well, you need to write some sort of dec2bin function, which shouldn't be too hard and then some sort of recursive FOR EACH...NEXT type loop which compares the bits in your network address with the bits in your IP address...

Alternatively, you could just write some sort of IF statement, which goes along the lines of;

Code:
If (intOctetOne = 10 AND intOctetTwo = 10 AND intOctetThree < 64) Then booInSubnet = TRUE

...which I suppose, in retrospect, would actually be a lot easier :)

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top