Solved

Check if given IP Addr falls in a given CIDR range

Posted on 2008-10-01
12
1,654 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I wrote this article to help simplify the process of combining multiple subnets. This can be used for route summarization also but there are other better ways to summarize routes, This article is a result of questions I participate in here at Ex…
Creating an OSPF network that automatically (dynamically) reroutes network traffic over other connections to prevent network downtime.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now