• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1886

# Check if given IP Addr falls in a given CIDR range

Hi,
I am a relative newbie to networking. I have to write a function in C++ to check if the given IP address falls in a given cidr range.
Appreciate if anyone can point to some tutorial on how to achive this or give me some ideas where to start.

Appreciate any help.
Thanks,
Mohit
0
Mohit_t
• 5
• 2
• 2
• +1
2 Solutions

Commented:
The easiest would be if you have the IP address as a 32bit value. You can then simple apply the appropriate mask for the range you want to check, and compare the value to that of the range.
0

Author Commented:
Hi Infinity08,
I am a relative newbie to network, so can you please expand a little bit more on your suggestion, are there any functions in C++ which can calculate the netmask of given ip address?
I will have a 32-bit address and have to check whether this address falls in the given cidr range
e.g. IP - 192.68.128.2 whether this falls in the cidr range 192.64.0.0/16.

Thanks,
Mohit
0

Commented:
0

Commented:
>> IP - 192.68.128.2 whether this falls in the cidr range 192.64.0.0/16.

The IP address 192.68.128.2 corresponds to the 32bit value 0xC0448002.
And 192.68.128.2 corresponds to the 32bit value 0xC0400000.
The /16 part is the mask, which corresponds to the 32bit value 0xFFFF0000.

The idea is to apply the mask to the IP address :

0xC0448002 & 0xFFFF0000 = 0xC0440000

and then compare it to 0xC0400000 :

0xC0440000 != 0xC0400000

so 192.68.128.2 does not fall in the CIDR range 192.64.0.0/16.

Now :

>> are there any functions in C++ which can calculate the netmask of given ip address?

Obviously, you can do the calculation yourself like I showed above. C++ in it self does not contain functionality to work with IP addresses. However, there are libraries that can do that.
Most commonly, you'd use a socket library for everything that has to do with socket programming. If you just need to check whether an IP falls into a certain range, and don't need to do anything else, then linking to a socket library might be a bit overkill, but you know best what you need :)

What is the input you get (in what form ?) ? And what is the output you require ?
0

Commented:
You can do it by calculating...
Lets say you want to check the IP 192.168.3.55 in the range: Start=192.168.1.1 ;  End=192.168.5.25.
You need to calculate: C*256+ D. That will be 3*256+55=823 for the IP address.
For the start and end of IP range, those values will be 1*256+1=257 for the start and  5*256+25=1305.
In order the IP address to be in the range, the value for the IP address should be between the values of start and end:
823 is greater than 257 (start) and less then 1305 (end) so this address fits in the range. :)

Regards,
Oliver

0

Commented:
Adding to the prevoius post, you can do the check for B field also...all you need to do is include the B part of the IP address in the calculation: B*256*256+C*256+D and compare the values. This will help you compare addresses like 192.68.128.2 in ranges like [192.64.0.0 till 192.69.0.0] where the B part is different also.

Regards,
Oliver
0

Commented:
I thought I already covered that ;)
0

Commented:
>> I an not sure whose ans. I followed. This has been a long time ago

That's not a reason to delete the question.

You should go over the question again, and re-evaluate which post(s) answered your question, and accept those.

If you close questions immediately instead of waiting 1.5 years, you won't have this problem ;)
0

Commented:
If the author doesn't get back to this question (I've waited a bit before posting my recommedation just for that reason), I'd PAQ the question with these two posts :

http:#22631275 (lakshmikarle) : some tutorials, as requested by the author
http:#22631955 (Infinity08) : specific explanation of how it is done for the author's requirements
0

Author Commented:
Hi,
The question was originally posted more than a year ago, I was a member then and after that I discontinued my membership. Now again I have some problems and so started my membership and as soon as the membership was started I think everybody got activiated and started commenting on how to proprely close the question, My earlier comment was because when I tried to delete the question I got a pop saying add you comment, I thought that was for closing the comment and not for publicly posting it.
Anyways sorry for my misunderstanding I request the moderators to close the connection and for points I will go with inifnity08's suggestion.
Thanks everyone for being so patient.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.