By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,121 Members | 642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,121 IT Pros & Developers. It's quick & easy.

Determining whether given IP exist in CIDR IP Range

ashitpro
Expert 100+
P: 542
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
Share this Question
Share on Google+
2 Replies

sicarie
Expert Mod 2.5K+
P: 4,677
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
Expert 100+
P: 542
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.