Link to home
Start Free TrialLog in
Avatar of Mohit_t
Mohit_t

asked on

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
Avatar of Infinity08
Infinity08
Flag of Belgium image

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.
Avatar of Mohit_t
Mohit_t

ASKER

Hi Infinity08,
Thanks for the prompt reply.
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
ASKER CERTIFIED SOLUTION
Avatar of lakshmikarle
lakshmikarle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
I thought I already covered that ;)
>> 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 ;)
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
Avatar of Mohit_t

ASKER

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.
I received more comments on this dead question than my live ones.
Thanks everyone for being so patient.