Expiring Todayâ€”Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

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

Posted on 2008-10-01
Medium Priority
1,759 Views
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
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
• 5
• 2
• 2
• +1

LVL 53

Expert Comment

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

ID: 22626414
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

LVL 1

Accepted Solution

lakshmikarle earned 500 total points
ID: 22631275
0

LVL 53

Assisted Solution

Infinity08 earned 500 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

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

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

ID: 22650691
I thought I already covered that ;)
0

LVL 53

Expert Comment

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

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

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.
Thanks everyone for being so patient.
0

## Featured Post

Question has a verified solution.

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

This article is a step by step guide on how to create a basic PTP link using Ubiquiti airOS devices. This guide can be used on the following Ubiquiti AirMAX devices. Nanostation, Bullets, AirBridge, Nanobeam, NanoBridge to name a few. Please review â€¦
AWS has developed and created its highly available global infrastructure allowing users to deploy and manage their estates all across the world through the use of the following geographical components Â  RegionsAvailability ZonesEdge LocationsÂ  Whâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month11 days, 8 hours left to enroll