Solved

Check if given IP Addr falls in a given CIDR range

Posted on 2008-10-01
12
1,741 Views
Last Modified: 2012-06-27
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
Comment
Question by:Mohit_t
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
12 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22617341
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 Comment

by:Mohit_t
ID: 22626414
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
0
 
LVL 1

Accepted Solution

by:
lakshmikarle earned 125 total points
ID: 22631275
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 125 total points
ID: 22631955
>> 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
 
LVL 1

Expert Comment

by:oliver_mk
ID: 22650269
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
 
LVL 1

Expert Comment

by:oliver_mk
ID: 22650427
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22650691
I thought I already covered that ;)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 25932109
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 25940692
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 Comment

by:Mohit_t
ID: 25944104
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question