468,512 Members | 1,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Determining whether given IP exist in CIDR IP Range

ashitpro
542 Expert 512MB
I want to check whether given IP exist in CIDR range..

e.g.

IP is 192.168.1.3
CIDR is 192.168.1.0/24

here according to CIDR
lower ip is 192.168.1.1
higher ip is 192.168.1.254

So if I give input as 192.168.1.3 and my cidr. It should return true.
Is there any facility in C/C++
Jan 30 '08 #1
2 6497
sicarie
4,677 Expert Mod 4TB
Doesn't seem like that would be a very difficult function to program, though I honestly haven't gone looking through many libraries to see if that does exist. Did you do a Google search for this?
Jan 30 '08 #2
ashitpro
542 Expert 512MB
Doesn't seem like that would be a very difficult function to program, though I honestly haven't gone looking through many libraries to see if that does exist. Did you do a Google search for this?
Yes, I did googling for this..
I got few solutions too..But they were too lengthy..
Eventually I've found out my own way to do it. I'd like to write here about it. May be helpful for other people.

e.g. CIDR is 192.168.9.0/24 and IP to check is 192.168.9.34.

step 1: convert 192.168.9.0 to unsigned long int by inet_aton function..Result will be LOWERIP.
step 2. calculate HIGHERIP = LOWERIP + (pow(2,32-mask value)-1)
Here mask value is 24 as per our example.
step 3. Convert your IP(address to check) in unigned long int format. Say it as MYIP
step 4. Now just check for LOWERIP < MYIP < HIGHERIP
If this is the case..then your IP present inside the range.
Jan 31 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by BigMan | last post: by
9 posts views Thread by Christopher Benson-Manica | last post: by
3 posts views Thread by Gareth Tonnessen | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.